Question: Inheritance and Polymorphism Create a superclass clothing with two variables size and color. Create three subclasses: shirt, pants, socks. For shirt on the size allow

Inheritance and Polymorphism

Create a superclass clothing with two variables size and color.

Create three subclasses: shirt, pants, socks.

For shirt on the size allow only S,M,L and XL. pants will have no size restrictions, socks also no restriction.

For socks, think about how you use a sock class, like for laundry folder (HINT)

Recall subclasses extend the superclass. Include constructors, getters, setters, and toString().

Write tester to show all three subclasses in action!

Here is an example tester file:

public class clothingTesterRuse { public static void main(String[] args) { // Test super class only clothing suit = new clothing(); clothing jersey = new clothing("medium", "blue"); System.out.println("clothing 1:" + suit); System.out.println("clothing 2:" + jersey); System.out.println(); // Test shirt class clothing blueShirt = new shirt("medium", "blue"); // sets size to null since not S, M, L or XL shirt pinkShirt = new shirt("Short Sleeves"); // shirt class has one field called style pinkShirt.setColor("Pink"); pinkShirt.setSize("M"); System.out.println("blueShirt " + blueShirt); System.out.println("pinkShirt " + pinkShirt); System.out.println(); // Test pants class pants blackPants = new pants(); blackPants.setColor("Black"); blackPants.setSize("M"); System.out.println("blackPants " + blackPants); System.out.println(); // Test sock class clothing purpleSocks = new socks("9-10", "Purple"); // socks has field pair, boolean default true in this constructor socks greenSock = new socks("7-8", "Green" ,false); System.out.println("purpleSocks " + purpleSocks); System.out.println("greenSocks " + greenSock); } } 

OUTPUT

clothing 1:clothing [size=null, color=null] 
clothing 2:clothing [size=medium, color=blue] 
blueShirt shirt [style=null, size=medium, color=blue] 
pinkShirt shirt [style=Short Sleeves, size=M, color=Pink] 
blackPants clothing [size=M, color=Black] 
purpleSocks socks [pair=true, size=9-10, color=Purple] 
greenSocks socks [pair=false, size=7-8, color=Green] 

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!