-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInheritanceTest.java
86 lines (56 loc) · 1.36 KB
/
InheritanceTest.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
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
class Parent{
protected int a;
protected int b;
Parent(int a, int b ){
this.a = a;
this.b = b;
}
protected void displayP(){
System.out.println(a+" "+" "+b);
}
protected static void test(){
System.out.println("This is static method");
}
}
class Child1 extends Parent{
int x;
int y;
Child1(int a, int b, int x, int y){
super(a,b); //calling constructor of Superclass P()
this.x = x;
this.y = y;
}
protected void displayC1(){
System.out.println(x+" "+" "+y);
}
}
class Child2 extends Parent{
protected int m,n;
Child2(int a, int b, int m, int n){
super(a,b); //calling constructor of Superclass P()
this.m = m;
this.n = n;
}
protected void displayC2(){
System.out.println(m+" "+" "+n);
}
}
class InheritanceTest{
public static void main(String[] args) {
Child2 c2 = new Child2(2,4,45,64);
Child1 c1 = new Child1(34,43,89,98);
//P p = c;
Parent.test();
Child2.test();
System.out.println("Values fron Child2 "+c2.m+" "+c2.a);
//p.displayC();
c2.displayP(); // from parent class
//c2.displayC1(); // from child1
c2.displayC2(); // from child2
System.out.println("Values fron Child1 ");
//p.displayC();
c1.displayP(); // from parent class
//c2.displayC1(); // from child1
c1.displayC1(); // from child2
}
}