Skip to content

Instantly share code, notes, and snippets.

@kenorb
Last active August 29, 2015 14:04
Show Gist options
  • Save kenorb/2b5fffa57b9ece392ed5 to your computer and use it in GitHub Desktop.
Save kenorb/2b5fffa57b9ece392ed5 to your computer and use it in GitHub Desktop.
ControlFlow Demo in Java.
/*
* 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