Question: 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
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 cannot reuse Animals greet, because there is no longer a greet definition in Animal. The greet method in Dog should produce the following two lines, using Animal's getName method to get the name:
Hello, my name is... and I'm a Dog!
In addition Dog should define a toString method that returns this:
, type = Dog
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();
All of these should work properly, using Dogs greet. You have just demonstrated polymorphism and dynamic or late binding!
IN JAVA :
* DO NOT MARK THIS CLASS PUBLIC */
class Animal /* convert this class to be abstract and have it implement Friendly */
{
private String name;
public Animal(String name) // constructor sets instance variable
{
this.name = name;
}
public String getName()
{
return name;
}
public void greet() /* change this method to be abstract, with no method body */
{
System.out.println("Hello, my name is " + name);
}
@Override
public String toString()
{
return "Animal: " + name;
}
}
/* DO NOT MARK THIS INTERFACE PUBLIC! */
interface Friendly
{
/* write this interface as shown in the slides in Week 11 */
}
public class Dog extends Animal // a class that extends Animal
{ // Dog now implicitly implements Friendly, since Animal does
/* write the Dog constructor that takes a String name and calls
the Animal constructor using super(), passing it the name */
@Override
public void greet()
{
/* fill in the greet method as required above */
}
@Override
public String toString()
{
return super.toString() + ", type = Dog";
}
public static void main(String[] args)
{
Dog fido = new Dog("fido");
fido.greet();
System.out.println(fido);
System.out.println();
Animal animal = new Dog("rover");
animal.greet();
System.out.println(animal);
System.out.println();
Friendly friendly = new Dog("wolfy");
friendly.greet();
System.out.println(friendly);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
