0% found this document useful (0 votes)
20 views7 pages

Superclass

The document defines a class hierarchy for food items, including a superclass 'Food' and subclasses 'Fruit', 'Vegetables', and 'Meat'. Each class has constructors, methods for preparation and selling, and a method to display information specific to the food type. The 'FoodTest' class demonstrates polymorphism by creating instances of each food type and invoking their methods.

Uploaded by

pakdot.tiki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

Superclass

The document defines a class hierarchy for food items, including a superclass 'Food' and subclasses 'Fruit', 'Vegetables', and 'Meat'. Each class has constructors, methods for preparation and selling, and a method to display information specific to the food type. The 'FoodTest' class demonstrates polymorphism by creating instances of each food type and invoking their methods.

Uploaded by

pakdot.tiki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

// Superclass: Food

// Represents a general food item with basic attributes and methods

class Food {

protected int caloriesPerServing; // Stores the number of calories per serving

protected String expiration; // Stores the expiration date of the food

// Overloaded Constructors

// Constructor that initializes food with given calories and expiration date

// Allows creating a food item with specific values

public Food(int caloriesPerServing, String expiration) {

this.caloriesPerServing = caloriesPerServing;

this.expiration = expiration;

// Default constructor that assigns default values

// Provides a default food item with generic values

public Food() {

this(0, "Unknown");

// Overridden Methods

// Method to prepare food (to be overridden by subclasses)

// Defines a general preparation process for food

public void prepare() {

System.out.println("Preparing food...");

// Method to simulate selling food (to be overridden by subclasses)

// Represents a generic selling process for food


public void sell() {

System.out.println("Selling food...");

// Method to display food details

// Prints information about the food item

public void displayInfo() {

System.out.println("Calories: " + caloriesPerServing + ", Expiration: " + expiration);

// Subclass: Fruit

// Represents fruit as a specific type of food

class Fruit extends Food {

private boolean isSweet; // Indicates whether the fruit is sweet

// Constructor that initializes a fruit with given values

// Allows creating a fruit item with specific values

public Fruit(int caloriesPerServing, String expiration, boolean isSweet) {

super(caloriesPerServing, expiration);

this.isSweet = isSweet;

// Default constructor that assigns default values

// Provides a default fruit item with generic values

public Fruit() {

this(50, "Unknown", true);

}
// Overriding prepare method for fruits

// Defines specific preparation steps for fruit

@Override

public void prepare() {

System.out.println("Washing and cutting fruit...");

// Overriding sell method for fruits

// Represents a specific selling process for fruits

@Override

public void sell() {

System.out.println("Selling fruit...");

// Overriding displayInfo to include fruit-specific details

// Displays additional details about the fruit

@Override

public void displayInfo() {

super.displayInfo();

System.out.println("Is Sweet: " + isSweet);

// Subclass: Vegetables

// Represents vegetables as a specific type of food

class Vegetables extends Food {

private boolean isLeafy; // Indicates whether the vegetable is leafy

// Constructor that initializes a vegetable with given values


// Allows creating a vegetable item with specific values

public Vegetables(int caloriesPerServing, String expiration, boolean isLeafy) {

super(caloriesPerServing, expiration);

this.isLeafy = isLeafy;

// Default constructor that assigns default values

// Provides a default vegetable item with generic values

public Vegetables() {

this(30, "Unknown", true);

// Overriding prepare method for vegetables

// Defines specific preparation steps for vegetables

@Override

public void prepare() {

System.out.println("Chopping vegetables...");

// Overriding sell method for vegetables

// Represents a specific selling process for vegetables

@Override

public void sell() {

System.out.println("Selling vegetables...");

// Overriding displayInfo to include vegetable-specific details

// Displays additional details about the vegetable

@Override
public void displayInfo() {

super.displayInfo();

System.out.println("Is Leafy: " + isLeafy);

// Subclass: Meat

// Represents meat as a specific type of food

class Meat extends Food {

private String meatType; // Specifies the type of meat

// Constructor that initializes meat with given values

// Allows creating a meat item with specific values

public Meat(int caloriesPerServing, String expiration, String meatType) {

super(caloriesPerServing, expiration);

this.meatType = meatType;

// Default constructor that assigns default values

// Provides a default meat item with generic values

public Meat() {

this(200, "Unknown", "Chicken");

// Overriding prepare method for meat

// Defines specific preparation steps for meat

@Override

public void prepare() {

System.out.println("Cooking meat...");
}

// Overriding sell method for meat

// Represents a specific selling process for meat

@Override

public void sell() {

System.out.println("Selling meat...");

// Overriding displayInfo to include meat-specific details

// Displays additional details about the meat

@Override

public void displayInfo() {

super.displayInfo();

System.out.println("Meat Type: " + meatType);

// Main class to test polymorphism

// Demonstrates polymorphic behavior of food subclasses

public class FoodTest {

public static void main(String[] args) {

// Creating instances of different food types using polymorphism

Food apple = new Fruit(52, "2025-04-10", true);

Food spinach = new Vegetables(23, "2025-03-15", true);

Food beef = new Meat(250, "2025-04-01", "Beef");

// Displaying details and actions for each food type

apple.displayInfo();
apple.prepare();

apple.sell();

System.out.println();

spinach.displayInfo();

spinach.prepare();

spinach.sell();

System.out.println();

beef.displayInfo();

beef.prepare();

beef.sell();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy