Last active
August 29, 2015 14:04
-
-
Save kenorb/2b5fffa57b9ece392ed5 to your computer and use it in GitHub Desktop.
ControlFlow Demo in Java.
This file contains hidden or 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
/* | |
* Control Flow Demo. | |
* | |
* Constructor call always starts from the top superclass down to subclasses. | |
*/ | |
class A { | |
// A(int x) { // It'll generate an error, as there is no default constructor defined. | |
A() { | |
// super(); // Invisible call which invoke superclass constructor (Object). | |
System.out.println("From class A"); | |
} | |
} | |
class B extends A { | |
B() { | |
// super(); // Invisible call which invoke superclass constructor (A). | |
// super(100); // If used, it'll try to invoke parameterised constructor of superclass. | |
System.out.println("From class B"); | |
// super(); // error: call to super must be first statement in constructor | |
} | |
} | |
class C extends B { | |
C() { | |
// super(); // Invisible call which invoke superclass constructor (B). | |
System.out.println("From class C"); | |
} | |
} | |
class ControlFlow { | |
public static void main(String[] args) { | |
new C(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment