Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 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. ---------------------------------------------------------------------------------------------------------------- Helping Notes: extends keyword is used in Java for Inheritance class Superclass { protected String name; protected String breed;

UMass Dartmouth CIS280 Spring 2023

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 }

UMass Dartmouth CIS280 Spring 2023

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. The TA will check the class hierarchy and the structure of OOP in your code.

Now, answer the following questions: 1. Declare a variable color, return type String, value = black with the keyword private: private String color="black" in the subclass. Now, try to print this variable from the superclasss main function. Define, why you cannot print it from the superclasss main function. 2. Now, add a public variable color in the superclass and assign value brown to it. public String color="brown". Then 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/Why not? 4. Change public to private of the getName() method in the subclass and try to access this method with the subclass object from the superclasss main function. Define why you cannot access that.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logistics Lifeline Supply Chain Strategies

Authors: Ehsan Sheroy

1st Edition

7419377502, 978-7419377503

More Books

Students also viewed these Databases questions