Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Animal {
- //private instance variable
- private final String name;
- //constructors
- public Animal(String name) {
- this.name = name;
- }
- public String toString() {
- return "Animal: Animal[name = " + name + "]";
- }
- }
- //next class
- public class Mammal extends Animal {
- //constructors
- public Mammal(String name) {
- super(name);
- }
- @Override
- public String toString() {
- return "Mammal: Mammal[ " + super.toString() + "]";
- }
- }
- //next class
- public class Cat extends Mammal {
- //constructors
- public Cat(String name) {
- super(name);
- }
- public void Greets() {
- System.out.println("Meow");
- }
- @Override
- public String toString() {
- return "Cat: Cat[ " + super.toString() + "]";
- }
- }
- //next class
- public class Dog extends Mammal {
- //constructor
- public Dog(String name) {
- super(name);
- }
- public void Greets() {
- System.out.println("Woof");
- }
- public void Greets2(String name) {
- System.out.println("Woooof");
- }
- @Override
- public String toString() {
- return "Dog: Dog " + super.toString() + "]";
- }
- }
- //test class
- public class testAnimal {
- public static void main(String[] args) {
- //to test animal class
- Animal a1 = new Animal("Rino");
- System.out.println(a1);
- System.out.println();
- //to test mammal class
- Mammal m1 = new Mammal("Dolphin");
- System.out.println(m1);
- System.out.println();
- //to test cat class
- Cat c1 = new Cat("Nyancheng");
- System.out.println(c1);
- c1.Greets();
- System.out.println();
- //to test dog class
- Dog d1 = new Dog("Golden Retriever");
- Dog d2 = new Dog("Husky");
- System.out.println(d1);
- d1.Greets();
- d2.Greets2("Husky");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement