Skip to content

Instantly share code, notes, and snippets.

@mverleg
Last active March 16, 2017 09:31
Show Gist options
  • Save mverleg/1088fbb5f21590da6691a6e84547bada to your computer and use it in GitHub Desktop.
Save mverleg/1088fbb5f21590da6691a6e84547bada to your computer and use it in GitHub Desktop.
Java inheritane of attributes and methods
/*
The question is simply: what is the output?
Extra: what is `instance.z` after line 8?
*/
public class TestQuestion1 {
public static void main(String[] args) {
SuperCls instance = new SubCls();
}
}
class SuperCls {
protected Integer x = 1;
protected Integer y = 3;
public SuperCls() {
System.out.println("super constructor");
b();
}
public void b() {
System.out.println("super b");
System.out.printf(String.format("x = %d, y = %d\n", x, y));
}
}
class SubCls extends SuperCls {
protected Integer x = 2;
public Integer z = 5;
public SubCls() {
y = 4;
System.out.println("sub constructor");
b();
}
@Override
public void b() {
super.b();
System.out.println("sub b");
System.out.printf(String.format("x = %d, y = %d\n", x, y));
}
}
@mverleg
Copy link
Author

mverleg commented Mar 16, 2017

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