Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3b5b8d6

Browse files
learn constructor chain in Java
1 parent43f5fbd commit3b5b8d6

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
packagelearnHeadFirstJava;
2+
3+
4+
publicclassAnimalextendsCreature {
5+
6+
publicAnimal() {
7+
System.out.println("Animal constructor is getting called!");
8+
}
9+
}
10+
11+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
packagelearnHeadFirstJava;
2+
3+
publicabstractclassCreature {
4+
5+
publicCreature() {
6+
System.out.println("Creature constructor is getting called!");
7+
}
8+
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
packagelearnHeadFirstJava;
2+
3+
publicclassDogextendsPet {
4+
5+
publicDog() {
6+
System.out.println("Dog constructor is getting called!");
7+
}
8+
9+
publicstaticvoidmain(String[]args) {
10+
DogmyDog =newDog();
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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
packagelearnHeadFirstJava;
2+
3+
4+
publicclassPetextendsAnimal {
5+
6+
publicPet() {
7+
System.out.println("Pet constructor is getting called!");
8+
}
9+
10+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp