0% found this document useful (0 votes)
7 views16 pages

Step Week-6 Lab Practice

The document outlines six lab problems focused on different aspects of object-oriented programming, including inheritance, constructor chaining, method overriding, and polymorphism. Each problem includes a problem statement, hints, and a solution in Java code demonstrating the concepts. The problems cover creating classes like Fruit, Phone, Bird, Color, Instrument, and Box, showcasing various inheritance and method functionalities.
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)
7 views16 pages

Step Week-6 Lab Practice

The document outlines six lab problems focused on different aspects of object-oriented programming, including inheritance, constructor chaining, method overriding, and polymorphism. Each problem includes a problem statement, hints, and a solution in Java code demonstrating the concepts. The problems cover creating classes like Fruit, Phone, Bird, Color, Instrument, and Box, showcasing various inheritance and method functionalities.
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

LAB- PRACTICE

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

🧪 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
🧪 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

🧪 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

🧪 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

🧪 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 [Link]() in overridden method first
● Add gift-specific functionality after super call
● Show enhanced behavior while preserving original

SOLUTION:-
1)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);
[Link] = variety;
}

public void displayInfo() {


[Link]("Apple Info:");
[Link]("Color: " + color);
[Link]("Taste: " + taste);
[Link]("Variety: " + variety);
}
}
public class Main {
public static void main(String[] args) {
Apple myApple = new Apple("Green", "Tart", "Granny Smith");
[Link]();
}
}
2) // Parent class
class Phone {
String brand;
String model;

public Phone(String brand, String model) {


[Link]("Phone class constructor is called.");
[Link] = brand;
[Link] = model;
}
}

// Child class
class SmartPhone extends Phone {
String operatingSystem;

// First constructor for SmartPhone


public SmartPhone(String brand, String model, String operatingSystem) {
super(brand, model); // Calls the parent class constructor
[Link]("SmartPhone class constructor with 3 parameters is
called.");
[Link] = operatingSystem;
}

// Second constructor for SmartPhone, demonstrating constructor chaining


public SmartPhone(String brand, String model) {
this(brand, model, "Android"); // Chains to the first SmartPhone
constructor
[Link]("SmartPhone class constructor with 2 parameters is
called.");
}
}

public class Main1 {


public static void main(String[] args) {
[Link]("Creating a SmartPhone object with all parameters:");
SmartPhone mySmartPhone = new SmartPhone("Samsung", "Galaxy S21",
"Android");

[Link]("\nCreating a SmartPhone object with 2 parameters


(using chaining):");
SmartPhone anotherSmartPhone = new SmartPhone("Apple", "iPhone
13");
}
}
3) / Parent class
class Bird {
public void fly() {
[Link]("The bird is flying.");
}
}

// Child class 1
class Penguin extends Bird {
@Override
public void fly() {
[Link]("A penguin cannot fly, but it can swim very well!");
}
}

// Child class 2
class Eagle extends Bird {
@Override
public void fly() {
[Link]("An eagle soars high in the sky.");
}
}

public class Main2 {


public static void main(String[] args) {
// Create an array of Bird references
Bird[] birds = new Bird[3];

// Populate the array with different types of birds


birds[0] = new Bird();
birds[1] = new Penguin();
birds[2] = new Eagle();

// Use a loop to call the fly() method on each object,


// demonstrating polymorphism
for (Bird bird : birds) {
[Link]();
}
}
}
4) // Base class
class Color {
protected String name;

public Color(String name) {


[Link]("Color constructor called.");
[Link] = name;
}
}

// Subclass of Color
class PrimaryColor extends Color {
protected int intensity;

public PrimaryColor(String name, int intensity) {


super(name); // Chains to Color's constructor
[Link]("PrimaryColor constructor called.");
[Link] = intensity;
}
}

// Subclass of PrimaryColor
class RedColor extends PrimaryColor {
private String shade;

public RedColor(String name, int intensity, String shade) {


super(name, intensity); // Chains to PrimaryColor's constructor
[Link]("RedColor constructor called.");
[Link] = shade;
}

public void displayProperties() {


[Link]("Color: " + name);
[Link]("Intensity: " + intensity);
[Link]("Shade: " + shade);
}
}

public class Main3 {


public static void main(String[] args) {
RedColor myRed = new RedColor("Red", 90, "Crimson");
[Link]("\nDisplaying object properties:");
[Link]();
}
}
5) // Base class
class Instrument {
protected String name;
protected String material;

public Instrument(String name, String material) {


[Link] = name;
[Link] = material;
}

public void play() {


[Link]("The " + name + " is making a sound.");
}
}

// Child class 1
class Piano extends Instrument {
private int numberOfKeys;

public Piano(String name, String material, int numberOfKeys) {


super(name, material);
[Link] = numberOfKeys;
}

@Override
public void play() {
[Link]("The " + name + " with " + numberOfKeys + " keys is
being played.");
}
}

// 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]("The " + name + " with " + numberOfStrings + " strings
is strummed.");
}
}

// Child class 3
class Drum extends Instrument {
private boolean isElectronic;

public Drum(String name, String material, boolean isElectronic) {


super(name, material);
[Link] = isElectronic;
}

@Override
public void play() {
if (isElectronic) {
[Link]("The " + name + " is played electronically.");
} else {
[Link]("The " + name + " is being hit.");
}
}
}
public class Main4 {
public static void main(String[] args) {
// Create an array of Instrument references
Instrument[] band = new Instrument[3];

// Populate the array with objects of different subclasses


band[0] = new Piano("Grand Piano", "Wood", 88);
band[1] = new Guitar("Acoustic Guitar", "Wood", 6);
band[2] = new Drum("Snare Drum", "Metal", false);

// Loop through the array and call the play() method


for (Instrument instrument : band) {
[Link]();
}
}
}
6) class Box {
public void pack() {
[Link]("Item is packed in a standard box.");
}

public void unpack() {


[Link]("Item is unpacked from a standard box.");
}
}
class GiftBox extends Box {
@Override
public void pack() {
// Calls the parent's method first
[Link]();
[Link]("Adding ribbons and wrapping paper to the gift box.");
}

@Override
public void unpack() {
[Link]("Unwrapping the gift box first.");
// Calls the parent's method after unwrapping
[Link]();
}
}

public class Main5 {


public static void main(String[] args) {
[Link]("Actions for a regular box:");
Box regularBox = new Box();
[Link]();
[Link]();

[Link]("\nActions for a gift box:");


GiftBox giftBox = new GiftBox();
[Link]();
[Link]();
}

}
OUTPUT:-

You might also like