0% found this document useful (0 votes)
4 views36 pages

Java Inheritance Masterclass - The Ultimate OOPs Battle

The document presents a Java Inheritance Masterclass that covers various examples of object-oriented programming concepts. It includes the creation of base classes and their subclasses, demonstrating method overriding for different functionalities such as sound, speed, area, salary, and more. Each example is accompanied by code snippets illustrating the implementation of these concepts in Java.

Uploaded by

Soumik Karmakar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views36 pages

Java Inheritance Masterclass - The Ultimate OOPs Battle

The document presents a Java Inheritance Masterclass that covers various examples of object-oriented programming concepts. It includes the creation of base classes and their subclasses, demonstrating method overriding for different functionalities such as sound, speed, area, salary, and more. Each example is accompanied by code snippets illustrating the implementation of these concepts in Java.

Uploaded by

Soumik Karmakar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Inheritance Masterclass – The

Ultimate OOPs Battle

🐾 1. Animal Class
📝 Question
Write a Java program to create a base class called Animal (Animal Family) with a method
called Sound().​
Create two subclasses Bird and Cat. Override the Sound() method in each subclass to
make a specific sound for each animal.

💻 Code
// Base class

class Animal {

// Method to make sound

void sound() {

[Link]("Animal makes a sound");

// Bird class inheriting Animal

Soumik Karmakar - Object Oriented Programming with Java


class Bird extends Animal {

// Overriding sound method

@Override

void sound() {

[Link]("Bird chirps");

// Cat class inheriting Animal

class Cat extends Animal {

// Overriding sound method

@Override

void sound() {

[Link]("Cat meows");

// Main class

public class Main {

public static void main(String[] args) {

// Creating Bird object

Soumik Karmakar - Object Oriented Programming with Java


Animal a1 = new Bird();

// Creating Cat object

Animal a2 = new Cat();

// Calling methods

[Link]();

[Link]();

🚗 2. Vehicle Class
📝 Question
Write a Java program to create a class called Vehicle with a method called speedUp().​
Create two subclasses Car and Bicycle. Override the speedUp() method in each subclass
to increase the vehicle's speed differently.

💻 Code
// Base Vehicle class

class Vehicle {

int speed = 0;

// Method to increase speed

Soumik Karmakar - Object Oriented Programming with Java


void speedUp() {

speed += 10;

[Link]("Vehicle Speed: " + speed);

// Car class

class Car extends Vehicle {

// Overriding speedUp method

@Override

void speedUp() {

speed += 30;

[Link]("Car Speed: " + speed);

// Bicycle class

class Bicycle extends Vehicle {

// Overriding speedUp method

@Override

void speedUp() {

speed += 15;

Soumik Karmakar - Object Oriented Programming with Java


[Link]("Bicycle Speed: " + speed);

// Main class

public class Main {

public static void main(String[] args) {

Vehicle v1 = new Car();

Vehicle v2 = new Bicycle();

[Link]();

[Link]();

🔺 3. Shape Class
📝 Question
Write a Java program to create a base class called Shape with a method called
calculateArea().​
Create three subclasses: Circle, Rectangle, and Triangle. Override the
calculateArea() method in each subclass to calculate and return the shape's area.

💻 Code
Soumik Karmakar - Object Oriented Programming with Java
// Base Shape class

class Shape {

// Method to calculate area

double calculateArea() {

return 0;

// Circle class

class Circle extends Shape {

double radius = 5;

// Overriding calculateArea method

@Override

double calculateArea() {

return [Link] * radius * radius;

// Rectangle class

class Rectangle extends Shape {

Soumik Karmakar - Object Oriented Programming with Java


double length = 4;

double breadth = 6;

// Overriding calculateArea method

@Override

double calculateArea() {

return length * breadth;

// Triangle class

class Triangle extends Shape {

double base = 5;

double height = 8;

// Overriding calculateArea method

@Override

double calculateArea() {

return 0.5 * base * height;

// Main class

Soumik Karmakar - Object Oriented Programming with Java


public class Main {

public static void main(String[] args) {

Shape s1 = new Circle();

Shape s2 = new Rectangle();

Shape s3 = new Triangle();

[Link]("Circle Area: " + [Link]());

[Link]("Rectangle Area: " + [Link]());

[Link]("Triangle Area: " + [Link]());

👨‍💼 4. Employee Class


📝 Question
Write a Java program to create a class called Employee with a method called
calculateSalary().​
Create two subclasses Manager and Programmer. In each subclass, override the
calculateSalary() method to calculate and return the salary based on their specific roles.

💻 Code
// Base Employee class

class Employee {

Soumik Karmakar - Object Oriented Programming with Java


// Method to calculate salary

double calculateSalary() {

return 0;

// Manager class

class Manager extends Employee {

// Overriding salary method

@Override

double calculateSalary() {

return 80000;

// Programmer class

class Programmer extends Employee {

// Overriding salary method

@Override

double calculateSalary() {

return 60000;

Soumik Karmakar - Object Oriented Programming with Java


}

// Main class

public class Main {

public static void main(String[] args) {

Employee e1 = new Manager();

Employee e2 = new Programmer();

[Link]("Manager Salary: " + [Link]());

[Link]("Programmer Salary: " + [Link]());

⚽ 5. Sports Class
📝 Question
Write a Java program to create a base class called Sports with a method called play().​
Create three subclasses: Football, Basketball, and Rugby. Override the play() method
in each subclass to display a specific statement for each sport.

💻 Code
// Base Sports class

class Sports {

Soumik Karmakar - Object Oriented Programming with Java


// Method to play

void play() {

[Link]("Playing Sports");

// Football class

class Football extends Sports {

// Overriding play method

@Override

void play() {

[Link]("Playing Football");

// Basketball class

class Basketball extends Sports {

// Overriding play method

@Override

void play() {

[Link]("Playing Basketball");

Soumik Karmakar - Object Oriented Programming with Java


}

// Rugby class

class Rugby extends Sports {

// Overriding play method

@Override

void play() {

[Link]("Playing Rugby");

// Main class

public class Main {

public static void main(String[] args) {

Sports s1 = new Football();

Sports s2 = new Basketball();

Sports s3 = new Rugby();

[Link]();

[Link]();

[Link]();

Soumik Karmakar - Object Oriented Programming with Java


}

📐 6. Shape Class with Area and Perimeter


📝 Question
Write a Java program to create a class called Shape with methods getArea() and
getPerimeter().​
Create three subclasses: Circle, Rectangle, and Triangle. Override the methods to
calculate area and perimeter.

💻 Code
// Base Shape class

class Shape {

// Method for area

double getArea() {

return 0;

// Method for perimeter

double getPerimeter() {

return 0;

Soumik Karmakar - Object Oriented Programming with Java


// Circle class

class Circle extends Shape {

double radius = 5;

// Overriding area method

@Override

double getArea() {

return [Link] * radius * radius;

// Overriding perimeter method

@Override

double getPerimeter() {

return 2 * [Link] * radius;

// Rectangle class

class Rectangle extends Shape {

double length = 4;

double breadth = 6;

Soumik Karmakar - Object Oriented Programming with Java


// Overriding area method

@Override

double getArea() {

return length * breadth;

// Overriding perimeter method

@Override

double getPerimeter() {

return 2 * (length + breadth);

// Triangle class

class Triangle extends Shape {

double a = 3;

double b = 4;

double c = 5;

// Overriding area method

@Override

double getArea() {

return 6;

Soumik Karmakar - Object Oriented Programming with Java


}

// Overriding perimeter method

@Override

double getPerimeter() {

return a + b + c;

// Main class

public class Main {

public static void main(String[] args) {

Shape s1 = new Circle();

Shape s2 = new Rectangle();

Shape s3 = new Triangle();

[Link]("Circle Area: " + [Link]());

[Link]("Circle Perimeter: " + [Link]());

[Link]("Rectangle Area: " + [Link]());

[Link]("Rectangle Perimeter: " + [Link]());

[Link]("Triangle Area: " + [Link]());

Soumik Karmakar - Object Oriented Programming with Java


[Link]("Triangle Perimeter: " + [Link]());

🦅 7. Animal Movement and Sound


📝 Question
Write a Java program to create a base class called Animal with methods move() and
makeSound().​
Create two subclasses Bird and Panthera. Override the move() method in each subclass to
describe how each animal moves. Also, override the makeSound() method in each subclass to
make a specific sound for each animal.

💻 Code
// Base Animal class

class Animal {

// Method for movement

void move() {

[Link]("Animal moves");

// Method for sound

void makeSound() {

[Link]("Animal makes sound");

Soumik Karmakar - Object Oriented Programming with Java


}

// Bird class

class Bird extends Animal {

// Overriding move method

@Override

void move() {

[Link]("Bird flies in the sky");

// Overriding sound method

@Override

void makeSound() {

[Link]("Bird chirps");

// Panthera class

class Panthera extends Animal {

// Overriding move method

@Override

void move() {

Soumik Karmakar - Object Oriented Programming with Java


[Link]("Panthera runs fast");

// Overriding sound method

@Override

void makeSound() {

[Link]("Panthera roars");

// Main class

public class Main {

public static void main(String[] args) {

Animal a1 = new Bird();

Animal a2 = new Panthera();

[Link]();

[Link]();

[Link]();

[Link]();

Soumik Karmakar - Object Oriented Programming with Java


🎨 8. Shape Drawing Program
📝 Question
Write a Java program to create a base class called Shape with methods draw() and
calculateArea().​
Create three subclasses: Circle, Square, and Triangle. Override the draw() method in
each subclass to draw the respective shape, and override the calculateArea() method to
calculate and return the area of each shape.

💻 Code
// Base Shape class

class Shape {

// Method to draw shape

void draw() {

[Link]("Drawing Shape");

// Method to calculate area

double calculateArea() {

return 0;

// Circle class

Soumik Karmakar - Object Oriented Programming with Java


class Circle extends Shape {

double radius = 5;

// Overriding draw method

@Override

void draw() {

[Link]("Drawing Circle");

// Overriding area method

@Override

double calculateArea() {

return [Link] * radius * radius;

// Square class

class Square extends Shape {

double side = 4;

// Overriding draw method

@Override

Soumik Karmakar - Object Oriented Programming with Java


void draw() {

[Link]("Drawing Square");

// Overriding area method

@Override

double calculateArea() {

return side * side;

// Triangle class

class Triangle extends Shape {

double base = 5;

double height = 6;

// Overriding draw method

@Override

void draw() {

[Link]("Drawing Triangle");

// Overriding area method

Soumik Karmakar - Object Oriented Programming with Java


@Override

double calculateArea() {

return 0.5 * base * height;

// Main class

public class Main {

public static void main(String[] args) {

Shape s1 = new Circle();

Shape s2 = new Square();

Shape s3 = new Triangle();

[Link]();

[Link]("Circle Area: " + [Link]());

[Link]();

[Link]("Square Area: " + [Link]());

[Link]();

[Link]("Triangle Area: " + [Link]());

Soumik Karmakar - Object Oriented Programming with Java


🏦 9. BankAccount Class
📝 Question
Write a Java program to create a base class called BankAccount with methods deposit()
and withdraw().​
Create two subclasses SavingsAccount and CheckingAccount. Override the
withdraw() method in each subclass to impose different withdrawal limits and fees.

💻 Code
// Base BankAccount class

class BankAccount {

double balance = 10000;

// Method to deposit money

void deposit(double amount) {

balance += amount;

// Method to withdraw money

void withdraw(double amount) {

balance -= amount;

Soumik Karmakar - Object Oriented Programming with Java


// SavingsAccount class

class SavingsAccount extends BankAccount {

// Overriding withdraw method

@Override

void withdraw(double amount) {

// Withdrawal limit check

if (amount <= 5000) {

balance -= amount;

[Link]("Savings Balance: " + balance);

} else {

[Link]("Withdrawal limit exceeded");

// CheckingAccount class

class CheckingAccount extends BankAccount {

// Overriding withdraw method

@Override

void withdraw(double amount) {

Soumik Karmakar - Object Oriented Programming with Java


// Deducting fee

balance = balance - amount - 100;

[Link]("Checking Balance: " + balance);

// Main class

public class Main {

public static void main(String[] args) {

BankAccount b1 = new SavingsAccount();

BankAccount b2 = new CheckingAccount();

[Link](4000);

[Link](4000);

🦁 10. Animal Eating and Sound


📝 Question

Soumik Karmakar - Object Oriented Programming with Java


Write a Java program to create a base class called Animal with methods eat() and sound().​
Create three subclasses: Lion, Tiger, and Panther. Override the eat() method in each
subclass to describe what each animal eats. Also, override the sound() method to make a
specific sound for each animal.

💻 Code
// Base Animal class

class Animal {

// Method for eating

void eat() {

[Link]("Animal eats food");

// Method for sound

void sound() {

[Link]("Animal makes sound");

// Lion class

class Lion extends Animal {

// Overriding eat method

@Override

void eat() {

Soumik Karmakar - Object Oriented Programming with Java


[Link]("Lion eats meat");

// Overriding sound method

@Override

void sound() {

[Link]("Lion roars");

// Tiger class

class Tiger extends Animal {

// Overriding eat method

@Override

void eat() {

[Link]("Tiger eats flesh");

// Overriding sound method

@Override

void sound() {

[Link]("Tiger growls");

Soumik Karmakar - Object Oriented Programming with Java


}

// Panther class

class Panther extends Animal {

// Overriding eat method

@Override

void eat() {

[Link]("Panther hunts animals");

// Overriding sound method

@Override

void sound() {

[Link]("Panther snarls");

// Main class

public class Main {

public static void main(String[] args) {

Animal a1 = new Lion();

Animal a2 = new Tiger();

Soumik Karmakar - Object Oriented Programming with Java


Animal a3 = new Panther();

[Link]();

[Link]();

[Link]();

[Link]();

[Link]();

[Link]();

🏍️ 11. Vehicle Engine Program


📝 Question
Write a Java program to create a base class called Vehicle with methods startEngine()
and stopEngine().​
Create two subclasses Car and Motorcycle. Override the methods to start and stop the
engines differently.

💻 Code
// Base Vehicle class

class Vehicle {

Soumik Karmakar - Object Oriented Programming with Java


// Method to start engine

void startEngine() {

[Link]("Vehicle engine starts");

// Method to stop engine

void stopEngine() {

[Link]("Vehicle engine stops");

// Car class

class Car extends Vehicle {

// Overriding startEngine method

@Override

void startEngine() {

[Link]("Car engine starts with key");

// Overriding stopEngine method

@Override

void stopEngine() {

[Link]("Car engine stops smoothly");

Soumik Karmakar - Object Oriented Programming with Java


}

// Motorcycle class

class Motorcycle extends Vehicle {

// Overriding startEngine method

@Override

void startEngine() {

[Link]("Motorcycle starts with self-start");

// Overriding stopEngine method

@Override

void stopEngine() {

[Link]("Motorcycle engine stops");

// Main class

public class Main {

public static void main(String[] args) {

Vehicle v1 = new Car();

Soumik Karmakar - Object Oriented Programming with Java


Vehicle v2 = new Motorcycle();

[Link]();

[Link]();

[Link]();

[Link]();

🛢️ 12. Shape and Cylinder Program


📝 Question
Write a Java program to create a base class called Shape with methods draw() and
calculateArea().​
Create two subclasses Circle and Cylinder. Override the draw() method in each subclass
to draw the respective shape. Also, override the calculateArea() method in the Cylinder
subclass to calculate and return the total surface area of the cylinder.

💻 Code
// Base Shape class

class Shape {

// Method to draw shape

void draw() {

[Link]("Drawing Shape");

Soumik Karmakar - Object Oriented Programming with Java


}

// Method to calculate area

double calculateArea() {

return 0;

// Circle class

class Circle extends Shape {

double radius = 5;

// Overriding draw method

@Override

void draw() {

[Link]("Drawing Circle");

// Overriding area method

@Override

double calculateArea() {

return [Link] * radius * radius;

Soumik Karmakar - Object Oriented Programming with Java


}

// Cylinder class

class Cylinder extends Shape {

double radius = 5;

double height = 10;

// Overriding draw method

@Override

void draw() {

[Link]("Drawing Cylinder");

// Overriding area method

@Override

double calculateArea() {

// Formula for total surface area

return 2 * [Link] * radius * (radius + height);

// Main class

Soumik Karmakar - Object Oriented Programming with Java


public class Main {

public static void main(String[] args) {

Shape s1 = new Circle();

Shape s2 = new Cylinder();

[Link]();

[Link]("Circle Area: " + [Link]());

[Link]();

[Link]("Cylinder Surface Area: " + [Link]());

Soumik Karmakar - Object Oriented Programming with Java

You might also like