LAB PROBLEM 1: Fruit and Apple Classes
// Problem 1: Basic Single Inheritance
class Fruit {
protected String color;
protected String taste;
public Fruit(String color, String taste) {
[Link] = color;
[Link] = taste;
}
}
class Apple extends Fruit {
private String variety;
public Apple(String color, String taste, String variety) {
super(color, taste); // calling parent constructor
[Link] = variety;
}
public void showDetails() {
[Link]("Apple Color: " + color);
[Link]("Apple Taste: " + taste);
[Link]("Apple Variety: " + variety);
}
}
public class LabProblem1 {
public static void main(String[] args) {
Apple a = new Apple("Red", "Sweet", "Fuji");
[Link]();
}
}
LAB PROBLEM 2: Phone and SmartPhone (Constructor Chaining)
// Problem 2: Constructor Chaining with super()
class Phone {
protected String brand;
protected String model;
public Phone(String brand, String model) {
[Link] = brand;
[Link] = model;
[Link]("Phone constructor called: " + brand + " " + model);
}
}
class SmartPhone extends Phone {
private String operatingSystem;
public SmartPhone(String brand, String model, String os) {
super(brand, model); // call parent constructor
[Link] = os;
[Link]("SmartPhone constructor called: OS = " + os);
}
public void showDetails() {
[Link]("Brand: " + brand + ", Model: " + model + ", OS: " + operatingSystem);
}
}
public class LabProblem2 {
public static void main(String[] args) {
SmartPhone sp = new SmartPhone("Apple", "iPhone 15", "iOS");
[Link]();
}
}
LAB PROBLEM 3: Bird Flying Behavior (Method Overriding)
// Problem 3: Method Overriding
class Bird {
public void fly() {
[Link]("Bird is flying...");
}
}
class Penguin extends Bird {
@Override
public void fly() {
[Link]("Penguins cannot fly, they swim!");
}
}
class Eagle extends Bird {
@Override
public void fly() {
[Link]("Eagle soars high in the sky!");
}
}
public class LabProblem3 {
public static void main(String[] args) {
Bird[] birds = { new Bird(), new Penguin(), new Eagle() };
for (Bird b : birds) {
[Link](); // polymorphic call
}
}
}
LAB PROBLEM 4: Color Hierarchy Chain (Multilevel Inheritance)
// Problem 4: Multilevel Inheritance
class Color {
protected String name;
public Color(String name) {
[Link] = name;
[Link]("Color constructor called: " + name);
}
}
class PrimaryColor extends Color {
protected int intensity;
public PrimaryColor(String name, int intensity) {
super(name); // call parent constructor
[Link] = intensity;
[Link]("PrimaryColor constructor called: Intensity = " + intensity);
}
}
class RedColor extends PrimaryColor {
private String shade;
public RedColor(String name, int intensity, String shade) {
super(name, intensity);
[Link] = shade;
[Link]("RedColor constructor called: Shade = " + shade);
}
public void showDetails() {
[Link]("Name: " + name + ", Intensity: " + intensity + ", Shade: " + shade);
}
}
public class LabProblem4 {
public static void main(String[] args) {
RedColor red = new RedColor("Red", 80, "Crimson");
[Link]();
}
}
LAB PROBLEM 5: Musical Instrument Family (Hierarchical
Inheritance)
// Problem 5: Hierarchical Inheritance
class Instrument {
protected String name;
protected String material;
public Instrument(String name, String material) {
[Link] = name;
[Link] = material;
}
public void play() {
[Link]("Playing instrument: " + name);
}
}
class Piano extends Instrument {
private int keys;
public Piano(String material, int keys) {
super("Piano", material);
[Link] = keys;
}
@Override
public void play() {
[Link]("Playing Piano with " + keys + " keys.");
}
}
class Guitar extends Instrument {
private int strings;
public Guitar(String material, int strings) {
super("Guitar", material);
[Link] = strings;
}
@Override
public void play() {
[Link]("Strumming Guitar with " + strings + " strings.");
}
}
class Drum extends Instrument {
private String type;
public Drum(String material, String type) {
super("Drum", material);
[Link] = type;
}
@Override
public void play() {
[Link]("Beating Drum of type: " + type);
}
}
public class LabProblem5 {
public static void main(String[] args) {
Instrument[] instruments = {
new Piano("Wood", 88),
new Guitar("Metal", 6),
new Drum("Leather", "Bass Drum")
};
for (Instrument inst : instruments) {
[Link]();
}
}
}
LAB PROBLEM 6: Box and GiftBox Enhancement (Using super in
Overridden Methods)
// Problem 6: Using super in overridden methods
class Box {
public void pack() {
[Link]("Packing the box...");
}
public void unpack() {
[Link]("Unpacking the box...");
}
}
class GiftBox extends Box {
@Override
public void pack() {
[Link](); // call parent method
[Link]("Adding ribbons and wrapping paper for the gift!");
}
@Override
public void unpack() {
[Link](); // call parent method
[Link]("Opening gift wrapping...");
}
}
public class LabProblem6 {
public static void main(String[] args) {
GiftBox gb = new GiftBox();
[Link]();
[Link]();
}
}