Inheritance Practice Exercises
Inheritance Practice Exercises
Create a subclass called Cat that overrides the makeSound() method to bark.
The above code defines a Java class named 'Main' with a 'main' method. Inside the 'main'
method:
Then, the 'makeSound()' method is called on both the 'animal' and 'cat' objects:
'animal.makeSound()' calls the 'makeSound()' method of the 'Animal' class, which prints "The
animal makes a sound." to the console.
'cat.makeSound()' calls the overridden 'makeSound()' method of the 'Cat' class, which prints
"The cat quarrels." to the console.
Output:
An instance of the Vehicle class is created and stored in the variable vehicle.
An instance of the Car class is created and stored in the variable car.
The drive() method is called on the vehicle object, resulting in the output "Repairing a
vehicle."
The drive() method is called on the car object, resulting in the output "Repairing a car."
Output:
Repairing a vehicle
Repairing a car
3. Write a 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.
Output:
Explanation:
In the above exercise, the Shape class has a single method called getArea() that returns a
double value. The Rectangle class is a subclass of Shape and overrides the getArea() method
to calculate the area of a rectangle using the formula length x width. The Rectangle class
constructor sets length and width values.
Finally in the main() method we create an instance of the Rectangle class and call its
getArea() method to get the rectangle's area.
Flowchart:
4. Write a 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().
Output:
working as an employee!
Employee salary: 40000
Managing employees
Manager salary: 70000
Explanation:
In the above exercise, the Employee class has a work() method that prints a message and a
getSalary() method that returns the employee's salary. The HRManager subclass extends the
Employee class and overrides the work() method to display a different message. It adds a
method addEmployee() that prints a message indicating that a new employee is being added.
The Main class creates an instance of Employee and HRManager, calls the work() and
getSalary() methods, and also calls the addEmployee() method on the HRManager object.
Flowchart:
5. Write a 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.
Output:
Create a Bank Account object (A/c No. BA1234) with initial balance of $500:
Deposit $1000 into account BA1234:
New balance after depositing $1000: $1500.0
Withdraw $600 from account BA1234:
New balance after withdrawing $600: $900.0
Create a SavingsAccount object (A/c No. SA1234) with initial balance of $450:
Balance after trying to withdraw $300: $150.0
Create a SavingsAccount object (A/c No. SA1000) with initial balance of $300:
Try to withdraw $250 from SA1000!
Minimum balance of $100 required!
Balance after trying to withdraw $250: $300.0
Explanation:
The BankAccount class has a constructor that takes account number and balance as
arguments. It also has methods to deposit and withdraw money, and to check the account
balance.
In Main() method -
The main method begins by creating an instance of the BankAccount class with an account
number of "BA1234" and an initial balance of $500. It then deposits $1000 into the account
and displays the new balance. It then withdraws $600 from the account and displays the new
balance.
Next, the method creates an instance of the SavingsAccount class with an account number of
"SA1234" and an initial balance of $450. It then attempts to withdraw $300 from the account
and displays the new balance. Since the balance remains above the minimum $150 balance
required for the account, the withdrawal is successful.
Finally, the method creates another instance of the SavingsAccount class with an account
number of "SA1000" and an initial balance of $300. It then attempts to withdraw $250 from
the account, which would bring the balance below the minimum balance required for the
account. The method displays the new balance after the attempted withdrawal, which should
still be $300 since the withdrawal was unsuccessful.
Flowchart: