LAB PROBLEM 1: Fruit and Apple Classes
Topic: Basic Single Inheritance
Problem Statement:
Create a Fruit class with color and taste fields. Create an Apple class that extends
Fruit and adds variety field.
Hints:
● Use extends keyword for inheritance
● Make fields protected in parent class
● Test by creating Apple object and accessing inherited fields
Source Code
// Fruit.java
public class Fruit {
protected String color;
protected String taste;
public Fruit(String color, String taste) {
this.color = color;
this.taste = taste;
}
}
// Apple.java
public class Apple extends Fruit {
private String variety;
public Apple(String color, String taste, String variety) {
super(color, taste); // Constructor chaining
this.variety = variety;
}
public void displayInfo() {
System.out.println("Color: " + color);
System.out.println("Taste: " + taste);
System.out.println("Variety: " + variety);
}
public static void main(String[] args) {
Apple apple = new Apple("Red", "Sweet", "Fuji");
apple.displayInfo();
}
}
🧪 LAB PROBLEM 2: Phone and SmartPhone
Constructors
Topic: Constructor Chaining with super()
Problem Statement:
Create Phone class with brand and model. Create SmartPhone class extending Phone with
operatingSystem field. Use constructor chaining.
Hints:
● Add print statements in constructors to see execution order
● Use super() in child constructor
● Create objects using different constructor combinations
Source Code
// Phone.java
public class Phone {
protected String brand;
protected String model;
public Phone(String brand, String model) {
this.brand = brand;
this.model = model;
System.out.println("Phone Constructor: " + brand + " " + model);
}
}
// SmartPhone.java
public class SmartPhone extends Phone {
private String operatingSystem;
public SmartPhone(String brand, String model, String operatingSystem) {
super(brand, model); // Constructor chaining
this.operatingSystem = operatingSystem;
System.out.println("SmartPhone Constructor: " + operatingSystem);
}
public static void main(String[] args) {
SmartPhone smartphone = new SmartPhone("Apple", "iPhone 14", "iOS");
}
}
LAB PROBLEM 3: Bird Flying Behavior
Topic: Method Overriding with @Override
1
Problem Statement:
Create Bird class with fly() method. Create Penguin and Eagle classes that override
fly() method differently.
Hints:
● Use @Override annotation
● Make different implementations in each child class
● Test polymorphism with array of Bird references
Source code
// Bird.java
public class Bird {
public void fly() {
System.out.println("Bird is flying.");
}
}
// Penguin.java
public class Penguin extends Bird {
@Override
public void fly() {
System.out.println("Penguins cannot fly.");
}
}
// Eagle.java
public class Eagle extends Bird {
@Override
public void fly() {
System.out.println("Eagle is soaring high!");
}
public static void main(String[] args) {
Bird[] birds = new Bird[2];
birds[0] = new Penguin();
birds[1] = new Eagle();
for (Bird bird : birds) {
bird.fly(); // Polymorphism in action
}
}
}
🧪 LAB PROBLEM 4: Color Hierarchy Chain
Topic: Multilevel Inheritance
Problem Statement:
Create inheritance chain: Color → PrimaryColor → RedColor. Each class adds specific
properties and methods.
Hints:
● Color has name field
● PrimaryColor adds intensity field
● RedColor adds shade field
● Show constructor chaining through all levels
Source code
// Color.java
public class Color {
protected String name;
public Color(String name) {
this.name = name;
System.out.println("Color Constructor: " + name);
}
}
// PrimaryColor.java
public class PrimaryColor extends Color {
protected int intensity;
public PrimaryColor(String name, int intensity) {
super(name); // Constructor chaining
this.intensity = intensity;
System.out.println("PrimaryColor Constructor: Intensity " + intensity);
}
}
// RedColor.java
public class RedColor extends PrimaryColor {
private String shade;
public RedColor(String name, int intensity, String shade) {
super(name, intensity); // Constructor chaining
this.shade = shade;
System.out.println("RedColor Constructor: Shade " + shade);
}
public static void main(String[] args) {
RedColor red = new RedColor("Red", 8, "Crimson");
}
}
LAB PROBLEM 5: Musical Instrument Family
Topic: Hierarchical Inheritance
Problem Statement:
Create Instrument base class. Create Piano, Guitar, and Drum classes that all extend
Instrument.
Hints:
● Base class has common fields like name, material
● Each child adds specific fields (strings, keys, etc.)
● Test using array of Instrument references
Source code
// Instrument.java
public class Instrument {
protected String name;
protected String material;
public Instrument(String name, String material) {
this.name = name;
this.material = material;
}
public void play() {
System.out.println("Playing the " + name);
}
}
// Piano.java
public class Piano extends Instrument {
private int keys;
public Piano(String name, String material, int keys) {
super(name, material); // Constructor chaining
this.keys = keys;
}
public void play() {
super.play();
System.out.println("This piano has " + keys + " keys.");
}
}
// Guitar.java
public class Guitar extends Instrument {
private int strings;
public Guitar(String name, String material, int strings) {
super(name, material);
this.strings = strings;
}
public void play() {
super.play();
System.out.println("This guitar has " + strings + " strings.");
}
}
// Drum.java
public class Drum extends Instrument {
private String drumType;
public Drum(String name, String material, String drumType) {
super(name, material);
this.drumType = drumType;
}
public void play() {
super.play();
System.out.println("This drum is a " + drumType + " drum.");
}
public static void main(String[] args) {
Instrument[] instruments = new Instrument[3];
instruments[0] = new Piano("Piano", "Wood", 88);
instruments[1] = new Guitar("Guitar", "Wood", 6);
instruments[2] = new Drum("Drum", "Wood", "Snare");
for (Instrument instrument : instruments) {
instrument.play();
}
}
}
LAB PROBLEM 6: Box and Gift Box Enhancement
Topic: Using super in overridden methods
Problem Statement:
Create Box class with pack() and unpack() methods. Create GiftBox that overrides these
methods but still uses parent functionality.
Hints:
● Call super.pack() in overridden method first
● Add gift-specific functionality after super call
● Show enhanced behavior while preserving original
Source code
// Box.java
public class Box {
public void pack() {
System.out.println("Packing the box.");
}
public void unpack() {
System.out.println("Unpacking the box.");
}
}
// GiftBox.java
public class GiftBox extends Box {
@Override
public void pack() {
super.pack(); // Calling the parent method first
System.out.println("Adding a ribbon to the gift box.");
}
@Override
public void unpack() {
super.unpack(); // Calling the parent method first
System.out.println("Unwrapping the ribbon and opening the gift box.");
}
public static void main(String[] args) {
GiftBox giftBox = new GiftBox();
giftBox.pack();
giftBox.unpack();
}
}