1)Program code:
package [Link];
// File: [Link]
public class FruitAppleDemo {
public static void main(String[] args) {
// Create an Apple object
Apple apple = new Apple("Red", "Sweet", "Fuji");
// Access inherited and specific fields
[Link]("Color: " + [Link]); // inherited
[Link]("Taste: " + [Link]); // inherited
[Link]("Variety: " + [Link]); // specific to Apple
// Call methods
[Link]();
[Link]();
}
}
// ================= Parent Class =================
class Fruit {
protected String color;
protected String taste;
// Constructor
public Fruit(String color, String taste) {
[Link] = color;
[Link] = taste;
}
// Method
public void showFruitInfo() {
[Link]("Fruit color: " + color + ", taste: " + taste);
}
}
// ================= Child Class =================
class Apple extends Fruit {
protected String variety;
// Constructor
public Apple(String color, String taste, String variety) {
super(color, taste); // Call parent constructor
[Link] = variety;
}
// Method
public void showAppleInfo() {
[Link]("Apple variety: " + variety +
" | Color: " + color +
" | Taste: " + taste);
}
}
Output:
2)Program code:
package [Link];
// File: [Link]
public class PhoneSmartPhoneDemo {
public static void main(String[] args) {
[Link]("----- Test 1: Default SmartPhone Constructor -----
");
SmartPhone sp1 = new SmartPhone();
[Link]();
[Link]("\n----- Test 2: Parameterized SmartPhone
Constructor -----");
SmartPhone sp2 = new SmartPhone("Apple", "iPhone 15", "iOS");
[Link]();
[Link]("\n----- Test 3: Another SmartPhone -----");
SmartPhone sp3 = new SmartPhone("Samsung", "Galaxy S24", "Android");
[Link]();
}
}
// ================= Parent Class =================
class Phone {
protected String brand;
protected String model;
// Default constructor
public Phone() {
[Link] = "Unknown";
[Link] = "Unknown";
[Link]("Phone default constructor called");
}
// Parameterized constructor
public Phone(String brand, String model) {
[Link] = brand;
[Link] = model;
[Link]("Phone parameterized constructor called");
}
}
// ================= Child Class =================
class SmartPhone extends Phone {
private String operatingSystem;
// Default constructor
public SmartPhone() {
super(); // Call parent default constructor
[Link] = "Unknown OS";
[Link]("SmartPhone default constructor called");
}
// Parameterized constructor
public SmartPhone(String brand, String model, String operatingSystem) {
super(brand, model); // Call parent parameterized constructor
[Link] = operatingSystem;
[Link]("SmartPhone parameterized constructor called");
}
// Method to display details
public void showDetails() {
[Link]("Brand: " + brand + ", Model: " + model +
", OS: " + operatingSystem);
}
}
Output:
3)Program code:
package [Link];
// File: [Link]
public class BirdFlyingDemo {
public static void main(String[] args) {
// Array of Bird references
Bird[] birds = new Bird[3];
birds[0] = new Bird();
birds[1] = new Penguin();
birds[2] = new Eagle();
[Link]("----- Bird Flying Behaviors -----");
for (Bird b : birds) {
[Link](); // Polymorphism: actual object method is called
}
}
}
// ================= Parent Class =================
class Bird {
public void fly() {
[Link]("Bird is flying in the sky...");
}
}
// ================= Child Class 1 =================
class Penguin extends Bird {
@Override
public void fly() {
[Link]("Penguins cannot fly, they swim!");
}
}
// ================= Child Class 2 =================
class Eagle extends Bird {
@Override
public void fly() {
[Link]("Eagle soars high in the sky!");
}
}
Output:
4)Program code:
package [Link];
// File: [Link]
public class ColorHierarchyDemo {
public static void main(String[] args) {
[Link]("----- Creating RedColor object -----");
RedColor red = new RedColor("Red", 90, "Dark Red");
[Link]("\n----- Displaying Properties -----");
[Link](); // from Color
[Link](); // from PrimaryColor
[Link](); // from RedColor
}
}
// ================= Base Class =================
class Color {
protected String name;
public Color(String name) {
[Link] = name;
[Link]("Color constructor: Creating color " + name);
}
public void showColor() {
[Link]("Color: " + name);
}
}
// ================= Intermediate Class =================
class PrimaryColor extends Color {
protected int intensity; // 0–100
public PrimaryColor(String name, int intensity) {
super(name); // calls Color constructor
[Link] = intensity;
[Link]("PrimaryColor constructor: Adding intensity " +
intensity);
}
public void showPrimaryColor() {
[Link]("Primary Color: " + name + ", Intensity: " +
intensity);
}
}
// ================= Derived Class =================
class RedColor extends PrimaryColor {
private String shade;
public RedColor(String name, int intensity, String shade) {
super(name, intensity); // calls PrimaryColor constructor
[Link] = shade;
[Link]("RedColor constructor: Creating shade " + shade);
}
public void showRedColor() {
[Link]("Red Color: " + name + ", Intensity: " + intensity
+ ", Shade: " + shade);
}
}
Output:
5)Program code:
package [Link];
// File: [Link]
public class InstrumentFamilyDemo {
public static void main(String[] args) {
// Create different instruments
Instrument piano = new Piano("Piano", "Wood", 88);
Instrument guitar = new Guitar("Guitar", "Maple Wood", 6);
Instrument drum = new Drum("Drum", "Metal", "Bass Drum");
// Store in array of Instrument references
Instrument[] instruments = { piano, guitar, drum };
[Link]("----- Instrument Performances -----");
for (Instrument inst : instruments) {
[Link]();
[Link](); // Polymorphism: actual child method is called
[Link]();
}
}
}
// ================= Base Class =================
class Instrument {
protected String name;
protected String material;
public Instrument(String name, String material) {
[Link] = name;
[Link] = material;
}
public void play() {
[Link]("Instrument is playing a sound...");
}
public void showInfo() {
[Link]("Instrument: " + name + ", Material: " + material);
}
}
// ================= Child Class 1 =================
class Piano extends Instrument {
private int keys;
public Piano(String name, String material, int keys) {
super(name, material);
[Link] = keys;
}
@Override
public void play() {
[Link]("Piano is playing melody with " + keys + " keys.");
}
public void pressPedal() {
[Link]("Piano pedal pressed for sustain.");
}
}
// ================= Child Class 2 =================
class Guitar extends Instrument {
private int numberOfStrings;
public Guitar(String name, String material, int numberOfStrings) {
super(name, material);
[Link] = numberOfStrings;
}
@Override
public void play() {
[Link]("Guitar is strumming chords with " +
numberOfStrings + " strings.");
}
public void tuneStrings() {
[Link]("Guitar strings are being tuned.");
}
}
// ================= Child Class 3 =================
class Drum extends Instrument {
private String drumType;
public Drum(String name, String material, String drumType) {
super(name, material);
[Link] = drumType;
}
@Override
public void play() {
[Link]("Drum (" + drumType + ") is beating rhythm.");
}
public void hitWithSticks() {
[Link]("Drum is being hit with sticks.");
}
}
Output:
6)Program code:
package [Link];
// File: [Link]
public class BoxDemo {
public static void main(String[] args) {
[Link]("----- Normal Box -----");
Box box = new Box();
[Link]();
[Link]();
[Link]("\n----- Gift Box -----");
GiftBox giftBox = new GiftBox();
[Link]();
[Link]();
}
}
// ================= Base Class =================
class Box {
public void pack() {
[Link]("Box is being packed.");
}
public void unpack() {
[Link]("Box is being unpacked.");
}
}
// ================= Child Class =================
class GiftBox extends Box {
@Override
public void pack() {
[Link](); // keep original packing
[Link]("Adding ribbons and decorations.");
}
@Override
public void unpack() {
[Link](); // keep original unpacking
[Link]("Removing decorations and opening the gift.");
}
}
Output: