-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolymorphism.java
More file actions
47 lines (46 loc) · 1.33 KB
/
polymorphism.java
File metadata and controls
47 lines (46 loc) · 1.33 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
public class polymorphism {
public static void main(String[]args)
{
//polymorphism=many shape
// objects can identify as other objs
//objs can be treated as objs of a common superclass
// ((like dog identifies as animal also identfies as a organism can also be identifed as an object))
horse h=new horse();
donkey d=new donkey();
bull b= new bull();
//if u try to make an array of horse donkey bull like this :-
// horse[] horses={horse,donkey,bull}; shows error like horse cannot be converted to donkey {{one shape cant be converted to another shape}}
//similarly for others donkey[] or bull[] array
//so we create array of a common type in which horse,donkey and bull can come i.e. animals
animals[] a={h,d,b};
for(animals animal: a)
{
animal.ride();
}
}
}
abstract class animals{
abstract void ride();
}
class horse extends animals{
@Override
void ride()
{
System.out.println("You ride the horse");
}
}
class donkey extends animals{
@Override
void ride()
{
System.out.println("You ride the donkey");
}
}
class bull extends animals
{
@Override
void ride()
{
System.out.println("You ride the bull");
}
}