-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritanceDemo.java
More file actions
101 lines (84 loc) · 1.71 KB
/
Copy pathInheritanceDemo.java
File metadata and controls
101 lines (84 loc) · 1.71 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* A
/ \
B C
/
D
*/
class A
{
int a;
A() //Default Constructor
{
a=0;
}
A(int a) //Parametrized Constructor
{
this.a=a;
}
public void printValue()
{
System.out.println("a : "+a);
}
public void messageA()
{
System.out.println("Property of A");
}
}
class B extends A
{
int b;
B()
{
super(); //Invokes A's Default Constructor
b=0;
}
B(int a,int b)
{
super(a); //Invokes A's Parametrized Constructor
this.b=b;
}
public void printValue()
{
super.printValue(); //invokes A's printValue()
System.out.println("b : "+b);
}
}
class C extends A
{
int c;
C(int a,int c)
{
super(a); //Invokes A's Parametrized Constructor
this.c=c;
}
public void printValue()
{
super.printValue(); //invokes A's printValue()
System.out.println("c : "+c);
}
}
class D extends B
{
int d;
D(int a,int b,int d)
{
super(a,b); //Invokes B's Constructor
this.d=d;
}
public void printValue()
{
super.printValue(); //invokes B's printValue()
System.out.println("d : "+d);
}
}
public class InheritanceDemo //Class containing main() method should have the same name as that of the file.
{
public static void main(String[] args)
{
B objB=new B();
objB.messageA(); //Invokes parent A's messageA() method.
System.out.println("\n\n\n");
D objD=new D(10,20,40);
objD.printValue(); //Invokes D's printValue() as objD is an object of D
}
}