Skip to content

Instantly share code, notes, and snippets.

@wisehuang
Created September 22, 2019 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wisehuang/6bc72342e9665546b3079e427e983728 to your computer and use it in GitHub Desktop.
Save wisehuang/6bc72342e9665546b3079e427e983728 to your computer and use it in GitHub Desktop.
Tell me what's wrong and how to fix it
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);
}
}
@b97601007
Copy link

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment