-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolymorphismExample.java
More file actions
46 lines (37 loc) · 944 Bytes
/
PolymorphismExample.java
File metadata and controls
46 lines (37 loc) · 944 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
package class_ex;
/**
* Polymorphism (多型) 範例
*
* 動態繫結 (Dynamic Binding) 更讓你能夠在執行期以同一介面的多個物件交互替換,
* 這種替換性質就叫做多型 (Polymorphism)
*
* Reference:
* - https://nwpie.blogspot.com/2017/05/3-class-interface-abstract.html
*/
public class PolymorphismExample {
public static void main(String[] args) {
Animal2 animal2 = new Animal2();
animal2.sound();
Pig pig = new Pig();
pig.sound();
Dog dog = new Dog();
dog.sound();
}
}
class Animal2 {
public void sound() {
System.out.println("The animal makes a sound");
}
}
class Pig extends Animal2 {
@Override
public void sound() {
System.out.println("The pig says: wee wee");
}
}
class Dog extends Animal2 {
@Override
public void sound() {
System.out.println("The dog says: bow wow");
}
}