-
-
Save kenocvr/481d62e78b5d93f0cfedbee411473da8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.LinkedList; | |
public abstract class Animal | |
{ | |
public abstract void eat(); | |
} | |
class Fish extends Animal | |
{ | |
@Override | |
public void eat() | |
{ | |
System.out.println("I eat like a fish!"); | |
} | |
} | |
class Goldfish extends Fish | |
{ | |
@Override | |
public void eat() | |
{ | |
System.out.println("I eat like a goldfish!"); | |
} | |
} | |
class Dog extends Animal { | |
@Override | |
public void eat() | |
{ | |
System.out.println("I eat like a dog!"); | |
} | |
} | |
public class Program | |
{ | |
public static void main(String[] args) { | |
List<Animal> animals = new LinkedList<Animal>(); | |
animals.add(new Fish()); | |
animals.add(new Goldfish()); | |
animals.add(new Dog()); | |
for (Animal currentAnimal : animals) | |
{ | |
currentAnimal.eat(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment