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() {
[Link]("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() {
[Link]("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
[Link]();
// Create a Cat object
Cat myCat = new Cat();
// Call makeSound() on the Cat object
[Link]();
}
}
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() {
[Link]("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() {
[Link]("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
[Link]();
// Create a Car object
Car myCar = new Car();
// Call drive() on the Car object
[Link]();
}
}
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() {
[Link]("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) {
[Link] = width;
[Link] = 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 = [Link]();
[Link]("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 = [Link]();
[Link]("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() {
[Link]("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() {
[Link]("HR Manager is managing human resources");
}
// New method for HRManager to add an employee
void addEmployee() {
[Link]("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
[Link]();
double salary = [Link]();
[Link]("Employee's salary: " + salary);
// Create an HRManager object
HRManager hrManager = new HRManager();
// Call work() and getSalary() on the HRManager object
[Link]();
double hrManagerSalary = [Link]();
[Link]("HR Manager's salary: " + hrManagerSalary);
// Call addEmployee() on the HRManager object
[Link]();
}
}
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) {
[Link] = initialBalance;
}
// Method to deposit money into the account
void deposit(double amount) {
balance += amount;
[Link]("Deposited: " + amount);
displayBalance();
}
// Method to withdraw money from the account
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
[Link]("Withdrawn: " + amount);
displayBalance();
} else {
[Link]("Insufficient funds. Withdrawal not allowed.");
}
}
// Method to display the account balance
void displayBalance() {
[Link]("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) {
[Link](amount);
} else {
[Link]("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
[Link](50);
[Link](80);
[Link](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.