forked from pray3m/JavaPrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntimePolymorphism.java
More file actions
29 lines (26 loc) · 847 Bytes
/
Copy pathRuntimePolymorphism.java
File metadata and controls
29 lines (26 loc) · 847 Bytes
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
/* 10. Run the following program in Java, that demonstrate the concepts of method overriding and
run time polymorphism. NOTE: read the comment properly, it may confuse you. */
// Base Class
class Parent {
void show() {
System.out.println("Parent's show()");
}
}
// Inherited class
class Child extends Parent {
// This method overrides show() of Parent
//Override
void show() {
System.out.println("Child's show()");
}
}
public class RuntimePolymorphism {
public static void main(String[] args) {
// If a Parent type reference refers to a Parent object, then Parent's show is called
Parent obj1 = new Parent();
obj1.show();
// If a Parent type reference refers to a Child object Child's show() is called RUN TIME POLYMORPHISM.
Parent obj2 = new Child();
obj2.show();
}
}