4.12.4 Clothing Store

4.12.4 Clothing Store

This document is a Java programming assignment focused on object-oriented programming concepts, specifically inheritance. It includes a Clothing superclass and subclasses for TShirt, Sweatshirt, and Jeans, demonstrating key OOP principles. The content is structured with source code examples and explanations, making it a useful resource for students learning Java.

225
/ 4
4.12.4 Clothing Store
CodeHS — Java • Object-Oriented Programming • Inheritance
Assignment Description
In this exercise, you design and test several classes to represent different pieces of clothing in a clothing
store. You write a Clothing superclass, then create three subclasses (TShirt, Sweatshirt, and Jeans)
that extend it using inheritance. Finally, a ClothingTester class creates instances of each and prints them
out.
This assignment demonstrates key OOP concepts: classes and objects, inheritance (extends / super),
constructors, getter methods, and the use of the super() keyword to call a parent class constructor.
Class Hierarchy
Class Extends Fields Key Methods
Clothing — (base class) size, color getSize(), getColor()
TShirt Clothing fabric getFabric()
Sweatshirt Clothing hasHood hasHood()
Jeans Clothing — (color defaults to "blue") — (inherits from Clothing)
Source Code
Clothing.java
public class Clothing
{
public String size, color;
public Clothing(String size, String color)
{
this.size = size;
this.color = color;
}
public String getSize()
{
return size;
}
public String getColor()
{
return color;
}
}
TShirt.java
public class TShirt extends Clothing
{
private String fabric;
public TShirt(String size, String color, String fabric)
{
super(size, color);
this.fabric = fabric;
}
public String getFabric()
{
return fabric;
}
}
Sweatshirt.java
public class Sweatshirt extends Clothing
{
private boolean hasHood;
public Sweatshirt(String size, String color, boolean hasHood)
{
super(size, color);
this.hasHood = hasHood;
}
public boolean hasHood()
{
return hasHood;
}
}
Jeans.java
public class Jeans extends Clothing
{
public Jeans(String size)
{
super(size, "blue");
}
}
ClothingTester.java
public class ClothingTester
{
public static void main(String[] args)
{
TShirt myTShirt = new TShirt("Large", "White", "Polyester");
Sweatshirt mySweatshirt = new Sweatshirt("Large", "Black", true);
Jeans myJeans = new Jeans("Large");
Jeans myJeansTwo = new Jeans("Medium");
System.out.println(myTShirt);
System.out.println(mySweatshirt);
System.out.println(myJeans);
System.out.println(myJeansTwo);
}
}
Key OOP Concepts Demonstrated
Concept How It's Used
/ 4
End of Document
225

You May Also Like

Related of 4.12.4 Clothing Store