-
Notifications
You must be signed in to change notification settings - Fork 0
/
constructor_in_inheritance.java
52 lines (43 loc) · 1.17 KB
/
constructor_in_inheritance.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Base{
Base(){
System.out.println("I am a constructor!");
}
Base(int a){
System.out.println("I am an overloaded constructor of value "+a);
}
}
class Derived extends Base{
int u;
void geter(int v){
this.u= v;
}
int seter(){
return u;
}
Derived(){
// super(4);
System.out.println("I am a constructor of derived class!");
}
Derived(int x,int y){
super(x);//super keyword calls the constructor of super class!
System.out.println("I am an overloaded constructor of derived class!");
}
}
class ChildDerived extends Derived{
ChildDerived(){
System.out.println("I am a constructor of child of the derived class!");
}
ChildDerived(int x,int y,int z){
super(x,y);
System.out.println("I am an overloaded constructor of child of the derived class!");
}
}
public class constructor_in_inheritance{
public static void main(String[] args){
// Derived d= new Derived(4,5);
// d.geter(6);
// System.out.println(d.seter());
ChildDerived a= new ChildDerived(3,4,5);
a.geter(3);
}
}