-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntimePoly.java
More file actions
45 lines (45 loc) · 1.15 KB
/
RuntimePoly.java
File metadata and controls
45 lines (45 loc) · 1.15 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
import java.util.Scanner;
public class RuntimePoly {
public static void main(String[]args)
{
//runtime polymorphism= when the method that gets executed is decided at runtime based on the actual type of theobj.
Scanner sc=new Scanner(System.in);
animall animal;
System.out.println("Would you like a dog or a cat? (1=dog ,2=cat)");
int choice=sc.nextInt();
//deciding at the runtime which class to call
switch(choice)
{
case 1->{
animal=new dogg();
animal.speak();
}
case 2-> {
animal=new catt();
animal.speak();
}
default->System.out.println("invalid choice");
}
// dogg d= new dogg();
// catt c=new catt();
// d.speak();
// c.speak();
}
}
interface animall {
void speak();
}
class dogg implements animall{
@Override
public void speak()
{
System.out.println("The dog goes 'woof' ");
}
}
class catt implements animall{
@Override
public void speak()
{
System.out.println("The cat goes 'meow' ");
}
}