Skip to content

Commit a6f6bd0

Browse files
Constructors in Inheritance
1 parent 0b9e1b2 commit a6f6bd0

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Base1{
2+
Base1(){
3+
System.out.println("I am a constructor");
4+
}
5+
Base1(int x){
6+
System.out.println("I am an overloaded constructor with value of x as: " + x);
7+
}
8+
}
9+
10+
class Derived1 extends Base1{
11+
Derived1(){
12+
//super(0);
13+
System.out.println("I am a derived class constructor");
14+
}
15+
Derived1(int x, int y){
16+
super(x);
17+
System.out.println("I am an overloaded constructor of Derived with value of y as: " + y);
18+
}
19+
}
20+
21+
class ChildOfDerived extends Derived1{
22+
ChildOfDerived(){
23+
System.out.println("I am a child of derived constructor");
24+
}
25+
ChildOfDerived(int x, int y, int z){
26+
super(x, y);
27+
System.out.println("I am an overloaded constructor of Derived with value of z as: " + z);
28+
}
29+
}
30+
public class cwh_46_constructors_in_inheritance {
31+
public static void main(String[] args) {
32+
// Base1 b = new Base1();
33+
// Derived1 d = new Derived1();
34+
// Derived1 d = new Derived1(14, 9);
35+
// ChildOfDerived cd = new ChildOfDerived();
36+
ChildOfDerived cd = new ChildOfDerived(12, 13, 15);
37+
}
38+
}

0 commit comments

Comments
 (0)