Java OOP Herança
Hello, Java!
Superclasse
package com.java;
// Animal Superclass
public class Animal {
private String move;
private String feed;
public String getMove() {
return move;
}
public void setMove(String move) {
this.move = move;
}
public String getFeed() {
return feed;
}
public void setFeed(String feed) {
this.feed = feed;
}
}
Subclasses
package com.java;
// Bird subclass
public class Bird extends Animal {
private String sing;
public String getSing() {
return sing;
}
public void setSing(String sing) {
this.sing = sing;
}
}
package com.java;
// Dog subclass
public class Dog extends Animal {
private String bark;
public String getBark() {
return bark;
}
public void setBark(String bark) {
this.bark = bark;
}
}
Classe para testar a execução
package com.java;
// Testing class
public class Test {
public static void main(String[] args) {
Animal animal = new Animal();
animal.setFeed("Every animal eats");
System.out.println(animal.getFeed());
Dog dog = new Dog();
dog.setFeed("Dog eats dog food");
System.out.println(dog.getFeed());
}
}
Compilar e executar
# Compile
find . -name "*.java" | xargs javac -d bin
# Run
java -classpath bin com.java.view.Test