Inheritance and Polymorphism
Posted: Tue Dec 15, 2009 10:57 am
Can anyone help me with this assignment, I want to get the right answer then work through it to undestand.
thanks!
create an abstract base class called Pet. This class will have one attribute called name.
Then I want to Create set and get methods for name and Create an abstract method called speak.
Implement a constructor that initializes name.
Create another class called Cat that extends the class Pet. This class will have an attribute for weight. Create set and get methods for weight. Implement the speak method so it prints the name of the pet and prints “I’m a cat and I weigh” and the value of weight. Implement a constructor that send the value of name to the Pet constructor and initializes the weight.
Create another class called Dog that extends the class Pet. This class will have an attribute for weight. Create set and get methods for weight. Implement the speak method so it prints the name of the pet and prints “I’m a dog and I weigh” and the value of weight.. Implement a constructor that send the value of name to the Pet constructor and initializes the weight.
Use the following line in the speak method to display the name:
echo $this‐>getName();
The above line uses the current instance to call the method getName in the base class.
In the driver program create and array that consists of an instance of a cat and an instance of a dog. Use a for loop to get each element in the array and call its speak method.
thanks!
create an abstract base class called Pet. This class will have one attribute called name.
Then I want to Create set and get methods for name and Create an abstract method called speak.
Implement a constructor that initializes name.
Create another class called Cat that extends the class Pet. This class will have an attribute for weight. Create set and get methods for weight. Implement the speak method so it prints the name of the pet and prints “I’m a cat and I weigh” and the value of weight. Implement a constructor that send the value of name to the Pet constructor and initializes the weight.
Create another class called Dog that extends the class Pet. This class will have an attribute for weight. Create set and get methods for weight. Implement the speak method so it prints the name of the pet and prints “I’m a dog and I weigh” and the value of weight.. Implement a constructor that send the value of name to the Pet constructor and initializes the weight.
Use the following line in the speak method to display the name:
echo $this‐>getName();
The above line uses the current instance to call the method getName in the base class.
In the driver program create and array that consists of an instance of a cat and an instance of a dog. Use a for loop to get each element in the array and call its speak method.