-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolymorphism.java
More file actions
48 lines (43 loc) · 1001 Bytes
/
Polymorphism.java
File metadata and controls
48 lines (43 loc) · 1001 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
*/
public class Polymorphism
{
public static void main(String[] args)
{
//ability of an object to take on many forms
Deer d = new Deer();
Animal a = d;
Vegetarian v = d;
Object o = d;
d.Specification();
d.VegEat();
a.Specification();
//a.VegEat(); method does not exist in Animal Class
//v.Specification(); abstract method does not exist in Vegetarian interface class
v.VegEat();
//o.Specification();
//o.VegEat();
}
}
//interface
interface Vegetarian
{
public void VegEat();
}
//super class
class Animal
{
public void Specification()
{
System.out.println("I am animal.");
}
}
//sub class
class Deer extends Animal implements Vegetarian
{
public void VegEat()
{
System.out.println("I like to eat plants.");
}
}