-
-
Save wisehuang/6bc72342e9665546b3079e427e983728 to your computer and use it in GitHub Desktop.
Tell me what's wrong and how to fix it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vive.tellmetheanswer; | |
public class Main { | |
public static void main(String[] args) { | |
var o = new Child(); | |
o.i = 1; | |
o.j = 2; | |
o.display(); | |
} | |
} | |
class Parents { | |
public int i; | |
private int j; | |
} | |
class Child extends Parents { | |
void display() { | |
super.j = super.i + 1; | |
System.out.println(super.i + " " + super.j); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I’d like to fix my previous answer.
It’s not allowed to change or get private parameter j directly.
Following is the way to fix it:
To build a private setter for j in class Parents.
To override setter and change the modifier to public in class Child. Inside the public setter, call super.setter and pass the integer.
To substitute o.j = 2 with o.setJ(2).