Last active
March 16, 2017 09:31
-
-
Save mverleg/1088fbb5f21590da6691a6e84547bada to your computer and use it in GitHub Desktop.
Java inheritane of attributes and methods
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
/* | |
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solution: https://gist.github.com/mverleg/0f03c8850c08b264471e690d86a319aa