-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod_overRide.java
More file actions
142 lines (128 loc) · 2.93 KB
/
method_overRide.java
File metadata and controls
142 lines (128 loc) · 2.93 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package src.inheritance;
class a2 {
int i, j;
a2(int a, int b) {
i = a;
j = b;
}
void show() {
System.out.println("Value of i & j: " + i + " " + j + " respectively");
}
}
class a3 extends a2 {
int k;
a3(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
// super.show(); //this will show superclass's method show()
System.out.println("Value of k: " + k);
}
}
class overRide {
public static void main(String[] args) {
a3 obj = new a3(3, 6, 9);
obj.show(); // this will overRide superclass's show() method
}
}
class a4 {
int i, j;
a4(int a, int b) {
i = a;
j = b;
}
void show() {
System.out.println("Value of i & j: " + i + " " + j + " respectively");
}
}
class a5 extends a2 {
int k;
a5(int a, int b, int c) {
super(a, b);
k = c;
}
void show(String msg) {
System.out.println(msg + k);
}
}
class NONoverRide {
public static void main(String[] args) {
a5 obj = new a5(3, 6, 9);
obj.show(); //this will call superclass's method
obj.show("Value of k: "); //this will call subclass's method
}
}
//Dynamic Method Dispatch
class a6 {
void callme() {
System.out.println("Inside 1st method");
}
}
class a7 extends a6 {
void callme() {
System.out.println("Inside 2nd method");
}
}
class a8 extends a7 {
void callme() {
System.out.println("Inside 3rd method");
}
}
class dispatch {
public static void main(String[] args) {
a6 ob1 = new a6();
a7 ob2 = new a7();
a8 ob3 = new a8();
a6 r;//reference variable
r = ob1;//it acts as 'ob1' placeholder
r.callme();
r = ob2;//it acts as 'ob2' placeholder
r.callme();
r = ob3;//it acts as 'ob3' placeholder
r.callme();
}
}
class figure {
double dim1, dim2;
figure(double a, double b) {
dim1 = a;
dim2 = b;
}
double area() {
System.out.println("Area of figure is undefined");
return 0;
}
}
class rectangle extends figure {
rectangle(double a, double b) {
super(a, b);
}
double area() {
System.out.println("Area of rectangle");
return dim1 * dim2;
}
}
class triangle extends figure {
triangle(double a, double b) {
super(a, b);
}
double area() {
System.out.println("Area of triangle");
return (dim1 * dim2) /2;
}
}
class areasOfFigure {
public static void main(String[] args) {
figure o1 = new figure(3, 9);
rectangle o2 = new rectangle(5, 8);
triangle o3 = new triangle(15, 8);
figure fig;
fig = o1;
System.out.println("Area: " + fig.area());
fig = o2;
System.out.println("Area: " + fig.area());
fig = o3;
System.out.println("Area: " + fig.area());
}
}