repl.it
@kenanhancer/

PolymorphismDemo3

Java

No description

fork
loading
Files
  • Main.java
  • jdt.ls-java-project
Main.java
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
class Main {
  public static void main(String[] args) {
    Animal myAnimal = new Animal();
    Animal myDog = new Dog();
    Animal myCat = new Cat();

    myAnimal.sound();
    myDog.sound();
    myCat.sound();
  }
}

class Animal {
  public void sound() {
    System.out.println("The animal makes a sound");
  }
}

class Dog extends Animal {
  public void sound() {
    System.out.println("The dog says: bow wow");
  }
}

class Cat extends Animal {
  public void sound() {
    System.out.println("The cat says: meow");
  }
}
?