-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMethodOverriding.java
More file actions
71 lines (60 loc) · 1.29 KB
/
MethodOverriding.java
File metadata and controls
71 lines (60 loc) · 1.29 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
package methods;
public class MethodOverriding {
public static void main(String[] args) {
parent p = new parent();
p.getInfo();
child c = new child();
c.getInfo();
System.out.println("calling both simultaneously:");
c.showInfo();
parent pa = p.human();
child ch = c.human();
parent pc = new child();
pc.getInfo();
System.out.println(pc.i);
System.out.println(pc.j);
pc.getInfo();
pc.parentInfo();
// pc.childInfo();
p = c;
p.parentInfo();
p.human();
}
}
// parent class
class parent {
int i = 2;
int j = 3;
public void getInfo() {
System.out.println("parent information:");
System.out.println("parent class is the superclass");
}
parent human() {
System.out.println("inside parent");
return new parent();
}
public void parentInfo() {
System.out.println("hello, this is parent!");
}
}
// child class in which methods of parent class are overridden
class child extends parent {
int i = 1;
@Override
public void getInfo() {
System.out.println("child information: ");
System.out.println("child class is the subclass");
}
void showInfo() {
super.getInfo();
getInfo();
}
@Override
child human() {
System.out.println("inside child");
return new child();
}
public void childInfo() {
System.out.println("Hello, this is child!!");
}
}