-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVirtualExample.java
More file actions
57 lines (48 loc) · 1.56 KB
/
VirtualExample.java
File metadata and controls
57 lines (48 loc) · 1.56 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
48
49
50
51
52
53
54
55
56
57
package src;
import java.util.LinkedList;
// src.src.Animal parent class
class Animal {
// normal method acting like virtual methods.
public void eat() {
System.out.println("I eat like a generic src.src.Animal.");
}
}
// src.Fish inherited from src.src.Animal class.
class Fish extends Animal {
// Overriding the src.src.Animal class 'eat' method
@Override
public void eat() {
System.out.println("I eat like a fish.");
}
}
// Goldfish inherited from src.src.Animal class.
class GoldFish extends Animal {
// Overriding the src.src.Animal class 'eat' method
@Override
public void eat() {
System.out.println("I eat like a gold fish.");
}
}
// src.otherAnimals inherited from src.src.Animal class.
class otherAnimals extends Animal {
// no overriding of 'eat' virtual method.
// therefore, the parent class method will be
// printed on console.
}
// main src.example class.
public class VirtualExample {
// main driven method.
public static void main(String[] args) {
// Creating a linked list of type 'src.src.Animal' class.
LinkedList<Animal> animals = new LinkedList<>();
// Adding the src.Fish, src.GoldFish, src.src.Animal and src.otherAnimals class src.objects.
animals.add(new Fish());
animals.add(new GoldFish());
animals.add(new Animal());
animals.add(new otherAnimals());
// Using the enhanced or for-each loop to iterate over the linked list.
for (var animal : animals) {
animal.eat();
}
}
}