Inheritance
Inheritance
Output:
Some generic animal sound
Bark! I'm a cat.
2. Java program to create a class called Vehicle with a method
called drive(). Create a subclass called Car that overrides the
drive() method to print "Repairing a car".
// Vehicle class
class Vehicle {
// Method to simulate driving a vehicle
void drive() {
System.out.println("Driving the vehicle");
}
}
// Car class, which is a subclass of Vehicle
class Car extends Vehicle {
// Override the drive() method to simulate repairing a car
@Override
void drive() {
System.out.println("Repairing a car");
}
}
// Main class to test the Vehicle and Car classes
public class Main {
public static void main(String[] args) {
// Create a Vehicle object
Vehicle myVehicle = new Vehicle();
// Call drive() on the Vehicle object
myVehicle.drive();
// Create a Car object
Car myCar = new Car();
// Call drive() on the Car object
myCar.drive();
}
}
Output:
Driving the vehicle
Repairing a car
3. Java program to create a class called Shape with a method called
getArea(). Create a subclass called Rectangle that overrides the
getArea() method to calculate the area of a rectangle.
// Shape class
class Shape {
// Method to get the area (placeholder implementation)
double getArea() {
System.out.println("Area calculation not implemented for generic
shape");
return 0.0;
}
}
// Rectangle class, which is a subclass of Shape
class Rectangle extends Shape {
// Fields to store width and height of the rectangle
private double width;
private double height;
// Constructor for Rectangle class
Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
// Override the getArea() method to calculate the area of a
rectangle
@Override
double getArea() {
return width * height;
}
}
// Main class to test the Shape and Rectangle classes
public class Main {
public static void main(String[] args) {
// Create a Shape object (not specifying a specific shape)
Shape genericShape = new Shape();
// Call getArea() on the Shape object
double genericArea = genericShape.getArea();
System.out.println("Area of the generic shape: " + genericArea);
// Employee class
class Employee {
// Placeholder implementation for the work() method
void work() {
System.out.println("Employee is working");
}
// Placeholder implementation for the getSalary() method
double getSalary() {
return 0.0;
}
}
// HRManager class, which is a subclass of Employee
class HRManager extends Employee {
// Override the work() method for HRManager
@Override
void work() {
System.out.println("HR Manager is managing human resources");
}
// New method for HRManager to add an employee
void addEmployee() {
System.out.println("HR Manager is adding a new employee");
}
}
// Main class to test the Employee and HRManager classes
public class Main {
public static void main(String[] args) {
// Create an Employee object
Employee employee = new Employee();
// Call work() and getSalary() on the Employee object
employee.work();
double salary = employee.getSalary();
System.out.println("Employee's salary: " + salary);
// Create an HRManager object
HRManager hrManager = new HRManager();
// Call work() and getSalary() on the HRManager object
hrManager.work();
double hrManagerSalary = hrManager.getSalary();
System.out.println("HR Manager's salary: " + hrManagerSalary);
Output:
Deposited: 50.0
Current Balance: 250.0
Withdrawn: 80.0
Current Balance: 170.0
Withdrawal not allowed. Minimum balance of 100 must be maintained.