Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class Animal {
- protected def weight
- public Animal(int weight) {
- this.weight = weight
- }
- abstract String roar()
- }
- class Dog extends Animal {
- public Dog(int weight) {
- super(weight)
- }
- @Override
- String roar() {
- return "HAO HAO HAO"
- }
- }
- class Cat extends Animal {
- public Cat(int weight) {
- super(weight)
- }
- @Override
- String roar() {
- return "Miao Miii Mi"
- }
- }
- /*
- Aqui são declaradas duas variáveis com o mesmo tipo no caso Animal
- que possuem o método roar como abstrato que para cada objeto instanciado
- se obtém uma resposta diferente.
- */
- static void main(String[] args) {
- Animal dog = new Dog(32)
- Animal cat = new Cat(16)
- println("Dog with ${dog.weight} pounds make ${dog.roar()}")
- println("Cat with ${cat.weight} pounds make ${cat.roar()}")
- }
Add Comment
Please, Sign In to add comment