In cataloging systems, objects are grouped by shared characteristics. This project demonstrates Hierarchical Inheritance by establishing a base Fruit class. Both Apple and Banana subclasses inherit the fruitColor field and the printColor() method. This design allows for a unified way to handle different fruit types while maintaining the flexibility to add unique properties to specific fruits in the future without duplicating the core logic of the superclass.
- Common Supertype: Created a
Fruitclass as the root for all fruit types. - State Inheritance: Leveraged the
fruitColorfield in multiple distinct subclasses. - Method Sharing: Used the inherited
printColor()method to display specific data for different objects. - Scalability: Designed the architecture to support adding new fruits (e.g.,
Orange,Grape) easily. - Clean Architecture: Followed the package structure and separated entry point logic.
- Java 8+ (Inheritance, Member Access, Polymorphic Behavior)
- Fruit: The superclass defining name and color attribute and display logic.
- Apple / Banana: Specialized subclasses inheriting from the base.
- ShopLauncherApp: The bootstrap class for demonstrating fruit property management.
Color Apple: Red
Color Banana: Yellow
Project Structure:
JavaBasics_Task_287/
├── src/
│ └── com/yurii/pavlenko/
│ ├── Fruit.java
│ ├── Apple.java
│ ├── Banana.java
│ └── ShopLauncherApp.java
└── README.md
Code
package com.yurii.pavlenko;
public class ShopLauncherApp {
public static void main(String[] args) {
Apple apple = new Apple();
apple.fruitName = "Apple";
apple.fruitColor = "Red";
Banana banana = new Banana();
banana.fruitName = "Banana";
banana.fruitColor = "Yellow";
apple.printColor();
banana.printColor();
}
}package com.yurii.pavlenko;
public class Fruit {
public String fruitColor;
public String fruitName;
public void printColor() {
System.out.println("Color " + fruitName + ": " + fruitColor);
}
}package com.yurii.pavlenko;
public class Apple extends Fruit {
}package com.yurii.pavlenko;
public class Banana extends Fruit {
}This project is licensed under the MIT License.
Copyright (c) 2026 Yurii Pavlenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...
License: MIT