-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunTimePoly.java
More file actions
26 lines (26 loc) · 849 Bytes
/
RunTimePoly.java
File metadata and controls
26 lines (26 loc) · 849 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
package BasicJavaPrograms.PolyMorphism;
public class RunTimePoly {
public static void main(String[] args) {
// Creating an instance of the Dog class and calling the makeSound method
Animal myDog = new RunTimePoly().new Dog();
myDog.makeSound(); // Output: Dog barks
// Creating an instance of the Cat class and calling the makeSound method
Animal myCat = new RunTimePoly().new Cat();
myCat.makeSound(); // Output: Cat meows
}
static class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void makeSound() {
System.out.println("Cat meows");
}
}
}