-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPet.java
More file actions
41 lines (35 loc) · 984 Bytes
/
Pet.java
File metadata and controls
41 lines (35 loc) · 984 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
// ============================================
// Abstraction Example ~ Pet Sound System
// Author ~ Vikas Kumar
// Topic ~ OOP ~ Abstraction ~ Inheritance
// ============================================
// Abstract class ~ Cannot be instantiated
abstract class Animals {
// Abstract method ~ Must be implemented by child
abstract void sound();
}
// Child class 1 ~ Dog implements sound
class Dog extends Animals {
@Override
public void sound() {
System.out.println("Woof");
}
}
// Child class 2 ~ Cat implements sound
class Cat extends Animals {
@Override
public void sound() {
System.out.println("Meow");
}
}
// Main class ~ Program entry point
class Pet {
public static void main(String[] args) {
// Polymorphism style ~ Parent reference
Animals a1 = new Dog();
Animals a2 = new Cat();
// Calling overridden methods
a1.sound(); // Dog sound
a2.sound(); // Cat sound
}
}