-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.java
More file actions
29 lines (24 loc) · 819 Bytes
/
Main.java
File metadata and controls
29 lines (24 loc) · 819 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
package DynamicPolymorphism;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// dynamic --> after compilation (during runtime)
// A corvette is a corvette, and a car, and a vehicle, and an object
Scanner scanner = new Scanner(System.in);
Animal animal;
System.out.println("What animal do you want?");
System.out.println("(1=dog) or (2=cat): ");
int choice = scanner.nextInt();
if (choice == 1) {
animal = new Dog();
animal.speak();
} else if (choice == 2) {
animal = new Cat();
animal.speak();
} else {
animal = new Animal();
System.out.println("That choice was invalid");
animal.speak();
}
}
}