Skip to content

Instantly share code, notes, and snippets.

@AnEmortalKid
Created September 23, 2015 18:58
Show Gist options
  • Save AnEmortalKid/eea241d49c371015d1cf to your computer and use it in GitHub Desktop.
Save AnEmortalKid/eea241d49c371015d1cf to your computer and use it in GitHub Desktop.
public class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void speak() {
System.out.println("My name is " + getName() + " and I am an Animal");
}
}
public class AnimalTest {
public static void main(String[] args) {
Animal animal = new Animal("Joe");
Fox fox = new Fox("Steve");
Cat cat = new Cat();
animal.speak();
fox.speak();
cat.speak();
}
}
public class Cat extends Animal {
public Cat() {
// every cat is named max
super("Max");
}
@Override
public void speak() {
System.out.println("My name is " + getName() + " and I meow meow moew");
}
}
public class Fox extends Animal {
public Fox(String name) {
super(name);
}
@Override
public void speak() {
System.out.println("My name is " + getName() + " and I say: Ringalinga dinga dinga ding ding ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment