Question: In this lab, you will practice Encapsulation, Inheritance, Polymorphism, and Abstraction in Java. First, read the statements very carefully: You have a pet. We identify

 In this lab, you will practice Encapsulation, Inheritance, Polymorphism, and Abstraction
in Java. First, read the statements very carefully: You have a pet.
We identify our pet by a name, its' color, breed. Your pet

In this lab, you will practice Encapsulation, Inheritance, Polymorphism, and Abstraction in Java. First, read the statements very carefully: You have a pet. We identify our pet by a name, its' color, breed. Your pet is a cat (or a dog). It has a name, you know its' breed and you know its' height and weight too. 1. If we think 'pet' and 'cat' are two classes, then in OOP, how the inheritance may look like: "cat" inherits 'pet' or 'pet' inherits " cal"? (Answer to this question) 2. Declare two classes namely: Pet and Cat. 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 'OOP Polymorphism' idea to declare the attributes/ methods of each class. Example: override the getName() method in the subclasses. You also need to 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. Helping Notes: "extends' keyword is used in Java for Inheritance class Superclass 1 protected String namo; protected String breed; Superclass() /*please do not use the default constructor to assign values to the variables / > Superclass (String n, String by name breed - b: 1 public String getName() return name: 1 public String getBreed() return breed Now, the Subclass code class Subclassl 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 1 public String get Breed() return breed; //overridden method public String getName (Strings) returns; //overloaded method do not forget to add the getHeight() and get Weight() methods Now, answer the following questions: 1. Declare a variable 'color', return type 'String' value='black' with the keyword "private's private String color="black" in the subclass. Now, try to print this variable from superclass's main function. Define, why you cannot print it from the superclass's main function. 2. Now, add a public variable color' in the superclass and assign value 'brown' to it. public String color "brown". And, try to access this variable from the subclass with the keyword 'super! 3. Can you access the overloaded method in the subclass with the superclass object? Why not? 4. Change 'public' to 'private' of the 'getName(" method in the subclass and try to access this method with subclass object from the superclass's main function. Define why you cannot access that

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!