Question: Create and use an abstract class: Modify the Animal class to be abstract and change its greet method to be abstract: Changing Animal on in-class
Create and use an abstract class: Modify the Animal class to be abstract and change its greet method to be abstract:
Changing Animal on in-class slide 5 to be abstract should only take two changes. The greet method should no longer have a method body.
Also, write the Friendly interface shown in the slides and change Animal to implement it.
Create a simple Dog class that looks like the one shown in the inheritance slides and have it extend Animal. Its greet method must @Override the abstract greet method in Animal and fully define it. Note that Dog can not reuse Animals greet, because there is no longer a greet definition in Animal.
Write a main method either in Dog or in a separate class that creates a Dog object, assigns it to a Dog reference variable, and runs that objects greet method: Dog d = new Dog("fido"); d.greet();
Also in main assign that Dog object or another new one to an Animal reference variable and run its greet method: Animal a = d; a.greet();
Finally in main assign a Dog object to a Friendly reference variable and run its greet method: : Friendly f = d; f.greet();
public class Animal // a simple Animal class { private String name; public Animal(String name) // constructor sets instance variable { this.name = name; } public String getName() { return name; } public void greet() // we might want to generalize this behavior { System.out.println("Hello, my name is " + name); } @Override public String toString() { return "Animal: " + name; } } All of these should work properly, using Dogs greet. You have just demonstrated polymorphism and dynamic or late binding!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
