Created
December 13, 2017 19:15
-
-
Save penut85420/cf410eedeef939ee38c6e77f1bb882eb to your computer and use it in GitHub Desktop.
A note of Polymorphism.
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
abstract class Animal { | |
abstract String talk(); | |
} | |
class Cat extends Animal { | |
String talk() { | |
return "Meow!"; | |
} | |
} | |
class Dog extends Animal { | |
String talk() { | |
return "Woof!"; | |
} | |
} | |
static void doTalk(final Animal a) { | |
println(a.talk()); | |
} | |
static void main(String[] args) { | |
doTalk(new Cat()); | |
doTalk(new Dog()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment