“DEBASHIS SIR” Page |1
// Use the @Override annotation to
Java Inheritance indicate that this method overrides a
Programming - Animal with a method in the superclass
method called makeSound @Override
Write a Java program to create a class called // Define the makeSound method
Animal with a method called makeSound(). public void makeSound() {
Create a subclass called Cat that overrides // Print "The cat quarrels." to the
the makeSound() method to bark. console
This exercise shows how inheritance works [Link]("The cat quarrels.");
in Java programming language. Inheritance }
allows you to create new classes based on }
existing classes, inheriting their attributes The above code defines a Java class named
and behaviors. In this case, the 'Cat' class is "Cat" that extends another class called
a more specific implementation of the "Animal". Within the "Cat" class, there is a
'Animal' class, adding quarrel behavior. method called "makeSound()" that
Sample Solution: overrides the same method from the
Java Code: "Animal" class. When this method is called
// Define a public class named Animal for a 'Cat' object, it prints the message "The
public class Animal { cat quarrels." to the console. This means
// Define a public method named that 'Cat' is a specific type of 'Animal' and
makeSound has its own unique sound, which is defined
public void makeSound() { in this method.
// Print "The animal makes a sound." to // Define a public class named Main
the console public class Main {
[Link]("The animal makes a // Define the main method
sound."); public static void main(String[] args) {
} // Create an instance of Animal
} Animal animal = new Animal();
// Create an instance of Cat
The above code defines a Java class named Cat cat = new Cat();
"Animal". Within this class, there is a // Call the makeSound method on the
method called "makeSound()". When this animal instance
method is called, it prints the message "The [Link]();
animal makes a sound." to the console. // Call the makeSound method on the
Essentially, this class serves as a blueprint cat instance
for creating objects representing generic [Link]();
animals that can make a sound. However, }
the specific sound is not defined here. }
// Define a public class named Cat that The above code defines a Java class named
extends Animal 'Main' with a 'main' method. Inside the
public class Cat extends Animal { 'main' method:
An 'Animal' object named 'animal' is
created.
“DEBASHIS SIR” Page |2
A 'Cat' object named 'cat' is created. The above code defines a Java class named
Both 'Animal' and 'Cat' are classes defined Vehicle. Inside this class:
elsewhere in the code. They have a method There is a method called drive(), which
called 'makeSound()'. prints "Repairing a vehicle" to the console.
Then, the 'makeSound()' method is called on // Define the child class Car that extends
both the 'animal' and 'cat' objects: Vehicle
'[Link]()' calls the class Car extends Vehicle {
'makeSound()' method of the 'Animal' class, // Use the @Override annotation to
which prints "The animal makes a sound." to indicate that this method overrides a
the console. method in the superclass
'[Link]()' calls the overridden @Override
'makeSound()' method of the 'Cat' class, // Define the drive method
which prints "The cat quarrels." to the public void drive() {
console. // Print "Repairing a car" to the console
Output: [Link]("Repairing a car");
The animal makes a sound. }
The cat quarrels. }
Java Program to Demonstrate Method The above code defines a Java class named
Overriding with Vehicle and Car" Car, which is a child class that extends the
Last update on May 16 2025 13:06:47 parent class Vehicle. Inside this class:
(UTC/GMT +8 hours) There is a method named drive(), which
overrides the drive() method from the
Write a Java program to create a class called parent class Vehicle. The overridden method
Vehicle with a method called drive(). Create prints "Repairing a car" to the console.
a subclass called Car that overrides the // Define the main class
drive() method to print "Repairing a car". public class Main {
This program creates a class called 'Vehicle' // Define the main method
with a method called drive() and a subclass public static void main(String[] args) {
called Car that overrides the drive() method // Create an instance of Vehicle
to print "Repairing a car". Vehicle vehicle = new Vehicle();
Sample Solution: // Create an instance of Car
Java Code: Car car = new Car();
// Define the parent class Vehicle // Call the drive method on the vehicle
class Vehicle { instance
// Define a public method named drive [Link](); // Output: Repairing a
public void drive() { vehicle
// Print "Repairing a vehicle" to the // Call the drive method on the car
console instance
[Link]("Repairing a [Link](); // Output: Repairing a car
vehicle"); }
} }
} In the exercise above -
“DEBASHIS SIR” Page |3
An instance of the Vehicle class is created // Define the constructor that takes
and stored in the variable vehicle. length and width as parameters
An instance of the Car class is created and public Rectangle(double length, double
stored in the variable car. width) {
The drive() method is called on the vehicle // Assign the length parameter to the
object, resulting in the output "Repairing a instance variable length
vehicle." [Link] = length;
The drive() method is called on the car // Assign the width parameter to the
object, resulting in the output "Repairing a instance variable width
car." [Link] = width;
Output: }
Repairing a vehicle
Repairing a car // Use the @Override annotation to
Java Program to Demonstrate Method indicate that this method overrides a
Overriding with Shape and Rectangle method in the superclass
Last update on May 16 2025 13:07:30 @Override
(UTC/GMT +8 hours) // Define the getArea method that returns
a double
Write a Java program to create a class called public double getArea() {
Shape with a method called getArea(). // Return the area of the rectangle
Create a subclass called Rectangle that (length * width)
overrides the getArea() method to calculate return length * width;
the area of a rectangle. }
Sample Solution: }
Java Code: // Define the main class
// Define the parent class Shape public class Main {
public class Shape { // Define the main method
// Define a public method named getArea public static void main(String[] args) {
that returns a double // Create an instance of Rectangle with
public double getArea() { length 3.0 and width 10.0
// Return 0.0 as the default area Rectangle rectangle = new
return 0.0; Rectangle(3.0, 10.0);
} // Call the getArea method on the
} rectangle instance and store the result in
the area variable
// Define the child class Rectangle that double area = [Link]();
extends Shape // Print the area of the rectangle to the
public class Rectangle extends Shape { console
// Define private instance variables length [Link]("The area of the
and width rectangle is: " + area);
private double length; }
private double width; }
Output:
“DEBASHIS SIR” Page |4
The area of the rectangle is: 30.0
Employee class with methods called work, // [Link]
getSalary // Child class HRManager
Last update on May 16 2025 13:06:48 public class HRManager extends Employee {
(UTC/GMT +8 hours)
// Constructor to initialize the salary of
Write a Java program to create a class called the HRManager
Employee with methods called work() and public HRManager(int salary) {
getSalary(). Create a subclass called // Call the parent class constructor with
HRManager that overrides the work() the salary
method and adds a new method called super(salary);
addEmployee(). }
Sample Solution:
Java Code: // Overridden method to simulate the
// [Link] HRManager working
// Parent class Employee public void work() {
public class Employee { // Print a message indicating the
HRManager is managing employees
// Private field to store the salary of the [Link]("\nManaging
employee employees");
private int salary; }
// Constructor to initialize the salary of // Method to simulate adding a new
the employee employee
public Employee(int salary) { public void addEmployee() {
[Link] = salary; // Print a message indicating a new
} employee is being added
[Link]("\nAdding new
// Method to simulate the employee employee!");
working }
public void work() { }
// Print a message indicating the // [Link]
employee is working // Main class
[Link]("working as an public class Main {
employee!");
} // Main method
public static void main(String[] args) {
// Getter method to retrieve the salary of // Create an Employee object with a
the employee salary of 40000
public int getSalary() { Employee emp = new
return salary; Employee(40000);
}
}
“DEBASHIS SIR” Page |5
// Create an HRManager object with a Create a subclass called Cheetah that
salary of 70000 overrides the move() method to run.
HRManager mgr = new Sample Solution:
HRManager(70000); Java Code:
// [Link]
// Call the work method on the // Parent class Animal
Employee object
[Link](); // Define the Animal class
public class Animal {
// Print the salary of the Employee // Method to print a message indicating
object the animal moves
[Link]("Employee salary: " public void move() {
+ [Link]()); [Link]("Animal moves");
}
// Call the work method on the }
HRManager object
[Link](); // [Link]
// Child class Cheetah
// Print the salary of the HRManager
object // Define the Cheetah class, inheriting from
[Link]("Manager salary: " + Animal
[Link]()); public class Cheetah extends Animal {
// Override the move method to print a
// Call the addEmployee method on the cheetah-specific message
HRManager object @Override
[Link](); public void move() {
} [Link]("This cheetah is
} running!");
}
Output: }
working as an employee! // [Link]
Employee salary: 40000 // Main class
Managing employees // Define the Main class
Manager salary: 70000 public class Main {
// Main method to run the program
Adding new employee! public static void main(String[] args) {
Animal Class with a method move() // Create an instance of Animal
Last update on May 16 2025 13:06:49 Animal animal = new Animal();
(UTC/GMT +8 hours) // Call the move method on the Animal
instance
Write a Java program to create a class called [Link]();
Animal with a method named move().
“DEBASHIS SIR” Page |6
// Create an instance of Cheetah // Child class Circle
Cheetah cheetah = new Cheetah();
// Call the move method on the // Declare the Circle class which extends the
Cheetah instance Shape class
[Link](); public class Circle extends Shape {
}
} // Private instance variable for the radius
Output: of the circle
Animal moves private double radius;
This cheetah is running!
Create a class called Shape with methods // Constructor for the Circle class, taking
called getPerimeter() and getArea() the radius as a parameter
Last update on May 16 2025 13:07:32 public Circle(double radius) {
(UTC/GMT +8 hours) // Initialize the radius instance variable
[Link] = radius;
Write a Java program to create a class called }
Shape with methods called getPerimeter()
and getArea(). Create a subclass called Circle // Override the getPerimeter method
that overrides the getPerimeter() and from the superclass (Shape)
getArea() methods to calculate the area and @Override
perimeter of a circle. public double getPerimeter() {
Sample Solution: // Return the perimeter of the circle
Java Code: calculated as 2 * π * radius
// [Link] return 2 * [Link] * radius;
// Parent class Shape }
// Declare the Shape class // Override the getArea method from the
public class Shape { superclass (Shape)
@Override
// Public method to get the perimeter of public double getArea() {
the shape, returning a default value of 0.0 // Return the area of the circle
public double getPerimeter() { calculated as π * radius^2
return 0.0; return [Link] * radius * radius;
} }
}
// Public method to get the area of the // [Link]
shape, returning a default value of 0.0 // Main class
public double getArea() {
return 0.0; // Declare the Main class
} public class Main {
}
// Main method to execute the program
// [Link] public static void main(String[] args) {
“DEBASHIS SIR” Page |7
// Declare a double variable r and Radius of the circle=3.2
initialize it to 8.0 Perimeter: 20.106192982974676
double r = 8.0; Area: 32.169908772759484
// Create a Circle object named c1 with
radius r
Circle c1 = new Circle(r);
// Print the radius of the circle c1
[Link]("Radius of the
circle=" + r);
// Print the perimeter of the circle c1
[Link]("Perimeter: " +
[Link]());
// Print the area of the circle c1
[Link]("Area: " +
[Link]());
// Update the value of r to 3.2
r = 3.2;
// Create a Circle object named c2 with
radius r
Circle c2 = new Circle(r);
// Print the radius of the circle c2
[Link]("\nRadius of the
circle=" + r);
// Print the perimeter of the circle c2
[Link]("Perimeter: " +
[Link]());
// Print the area of the circle c2
[Link]("Area: " +
[Link]());
}
}
Output:
Radius of the circle=8.0
Perimeter: 50.26548245743669
Area: 201.06192982974676