Created
September 23, 2015 18:58
-
-
Save AnEmortalKid/eea241d49c371015d1cf 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
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"); | |
} | |
} |
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
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(); | |
} | |
} |
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
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"); | |
} | |
} |
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
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