0% found this document useful (0 votes)
417 views

Inheritance

The document contains 5 Java programs that demonstrate inheritance and method overriding in Java. Each program creates a base class with methods and subclasses that override the methods. The programs show subclasses overriding methods to change or add functionality specific to the subclass.

Uploaded by

aditttoo24
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)
417 views

Inheritance

The document contains 5 Java programs that demonstrate inheritance and method overriding in Java. Each program creates a base class with methods and subclasses that override the methods. The programs show subclasses overriding methods to change or add functionality specific to the subclass.

Uploaded by

aditttoo24
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
You are on page 1/ 7

1.

Java program to create a class called Animal with a method


called makeSound(). Create a subclass called Cat that overrides the
makeSound() method to bark.
// Animal class
class Animal {
// Method to make a generic animal sound
void makeSound() {
System.out.println("Some generic animal sound");
}
}
// Cat class, which is a subclass of Animal
class Cat extends Animal {
// Override the makeSound() method to make a cat bark
@Override
void makeSound() {
System.out.println("Bark! I'm a cat.");
}
}
// Main class to test the Animal and Cat classes
public class Main {
public static void main(String[] args) {
// Create an Animal object
Animal genericAnimal = new Animal();
// Call makeSound() on the Animal object
genericAnimal.makeSound();
// Create a Cat object
Cat myCat = new Cat();
// Call makeSound() on the Cat object
myCat.makeSound();
}
}

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);

// Create a Rectangle object with width 5 and height 10


Rectangle myRectangle = new Rectangle(5, 10);
// Call getArea() on the Rectangle object
double rectangleArea = myRectangle.getArea();
System.out.println("Area of the rectangle: " + rectangleArea);
}
}
Output: Area calculation not implemented for generic shape
Area of the rectangle: 50.0
4. Java program to create a class called Employee with methods
called work() and getSalary(). Create a subclass called HRManager
that overrides the work() method and adds a new method called
addEmployee().

// 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);

// Call addEmployee() on the HRManager object


hrManager.addEmployee();
}
}
Output:
Employee is working
Employee's salary: 0.0
HR Manager is managing human resources
HR Manager's salary: 0.0
HR Manager is adding a new employee
5. Java program to create a class known as "BankAccount" with
methods called deposit() and withdraw(). Create a subclass called
SavingsAccount that overrides the withdraw() method to prevent
withdrawals if the account balance falls below one hundred.
// BankAccount class
class BankAccount {
// Instance variable to store the account balance
private double balance;
// Constructor for BankAccount class
BankAccount(double initialBalance) {
this.balance = initialBalance;
}
// Method to deposit money into the account
void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount);
displayBalance();
}
// Method to withdraw money from the account
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("Withdrawn: " + amount);
displayBalance();
} else {
System.out.println("Insufficient funds. Withdrawal not allowed.");
}
}
// Method to display the account balance
void displayBalance() {
System.out.println("Current Balance: " + balance);
}
}
// SavingsAccount class, which is a subclass of BankAccount
class SavingsAccount extends BankAccount {
// Constructor for SavingsAccount class
SavingsAccount(double initialBalance) {
super(initialBalance);
}
// Override the withdraw() method to prevent withdrawals if the
balance falls below 100
@Override
void withdraw(double amount) {
if (getBalance() - amount >= 100) {
super.withdraw(amount);
} else {
System.out.println("Withdrawal not allowed. Minimum balance of 100
must be maintained.");
}
}
}

// Main class to test the BankAccount and SavingsAccount classes


public class Main {
public static void main(String[] args) {
// Create a SavingsAccount object with an initial balance of 200
SavingsAccount savingsAccount = new SavingsAccount(200);

// Deposit and withdraw from the savings account


savingsAccount.deposit(50);
savingsAccount.withdraw(80);
savingsAccount.withdraw(150);
}
}

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.

You might also like