Skip to content

Commit 3b5b8d6

Browse files
learn constructor chain in Java
1 parent 43f5fbd commit 3b5b8d6

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package learnHeadFirstJava;
2+
3+
4+
public class Animal extends Creature {
5+
6+
public Animal() {
7+
System.out.println("Animal constructor is getting called!");
8+
}
9+
}
10+
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package learnHeadFirstJava;
2+
3+
public abstract class Creature {
4+
5+
public Creature() {
6+
System.out.println("Creature constructor is getting called!");
7+
}
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package learnHeadFirstJava;
2+
3+
public class Dog extends Pet {
4+
5+
public Dog() {
6+
System.out.println("Dog constructor is getting called!");
7+
}
8+
9+
public static void main(String[] args) {
10+
Dog myDog = new Dog();
11+
System.out
12+
.println("\nThis is really calling as the book stated and as I expected,\n"
13+
+ "each method gets put onto the stack, up to the end of the inheritance "
14+
+ "tree, then the root constructor is called, "
15+
+ "then down to the bottom subclass's constructor is called!\nNice!\nProgram ended.");
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package learnHeadFirstJava;
2+
3+
4+
public class Pet extends Animal {
5+
6+
public Pet() {
7+
System.out.println("Pet constructor is getting called!");
8+
}
9+
10+
}

0 commit comments

Comments
 (0)