Question
In this exercise, we'll open a previously created project and further explore IS-A vs. HAS-A. Project Setup Open your IDE (Eclipse). Right-click on the Java
In this exercise, we'll open a previously created project and further explore IS-A vs. HAS-A.
Project Setup
- Open your IDE (Eclipse).
- Right-click on the Java Project, Lab-Inheritance and Select Copy.
3. Right-click anywhere in the blank space inside of the Package Explorer panel and select Paste.
4. Name the new project, Lab-HAS-A.
5. Create a new class Outfit within the com.revature.model package.
6. Within this Outfit class, create a single method, wear().
This method will not have any parameters nor will it return anything. It will simply print out. "I look fantastic".
Your new class should look like the following:
package com.revature.model; public class Outfit { public void wear() { System.out.println("I look fantastic"); } }
5. Open the file for your Doll class.
Add in a single Outfit field.
By including the line:
Outfit outfit = new Outfit();
Now, take note of the class's structure.
What are the relationships that are HAS-A and which ones are IS-A?
Note that a Doll HAS-AN Outfit and a Doll IS-A Toy.
Edit the Doll's makeTalk() method to call Outfit's wear() method as its first statement:
public void makeTalk() { this.outfit.wear(); System.out.println("Hi everyone! I AM-A doll"); }
Great. Now edit the same method to call the inherited play() method as the last statement.
public void makeTalk() { this.outfit.wear(); System.out.println("Hi everyone! I AM-A doll"); this.play() }
Notice the difference between how we call Outfit's wear() method versus Toy's play() method. We have to refer to Doll's instance of Outfit first and then call the wear() method, whereas we can directly call Toy's play() method because Doll has inherited it.
This completes the exercise. It is a subtle detail, but an important one in understanding how classes relate to one another.
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