Question
Helping Notes: extends keyword is used in Java for Inheritance class Superclass { protected String name; protected String breed; Superclass() { /*please do not use
Helping Notes: extends keyword is used in Java for Inheritance
class Superclass
{
protected String name;
protected String breed;
Superclass()
{
/*please do not use the default constructor to assign values to the
variables */
}
Superclass(String n, String b)
{
name = n;
breed = b;
}
public String getName()
{
return name;
}
public String getBreed()
{
return breed;
}
}
Now, the Subclass code
class Subclass1 extends Superclass
{
protected String name;
protected String breed;
protected int weight;
protected int height;
Subclass()
{
/*please do not use the default constructor to assign values to the
variables */
}
public String getName()
{
return name; //overridden method
}
public String getBreed()
{
return breed; //overridden method
}
public String getName(String s)
{
return s; //overloaded method
}
/* do not forget to add the getHeight() and getWeight() methods
}
Your code may have a similar structure. You can declare any local or global variables if you need. Under the source folder, each class will have separate class files.
You have a pet. We identify our pets by a name, its color, and breed. Your pet is either a cat or a dog (your call). It has a name, you know its breed, and you know its height and weight too. 1. If we think that 'pet' and 'cat' are two classes, then in OOP, how would the inheritance look like: 'cat' inherits 'pet' or 'pet' inherits 'cat'? (Answer this question first) 2. Declare two classes namely: Pet and Cat (or if your pet is a dog, name the class Dog). 3. Write the Java code of this inheritance: Superclass to subclass. 4. Declare the variables/methods in respective classes based on the above statements. 5. Use the idea of 'OOP Polymorphism' to declare the attributes/methods of each class. For example: overriding the getName() method in the subclasses. You must also write one 'overload' method in the subclass. 6. Write a method in the subclass from where you will try to access the superclass attributes. 7. Both class files should have main methods. Create objects of each class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started