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

Inheritance Practice Exercises

The document outlines several Java programming exercises involving class creation and method overriding. It includes examples of an Animal class with a Cat subclass, a Vehicle class with a Car subclass, a Shape class with a Rectangle subclass, an Employee class with an HRManager subclass, and a BankAccount class with a SavingsAccount subclass. Each example demonstrates the use of inheritance and polymorphism, along with expected outputs and explanations.

Uploaded by

jeevanmunna0892
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Inheritance Practice Exercises

The document outlines several Java programming exercises involving class creation and method overriding. It includes examples of an Animal class with a Cat subclass, a Vehicle class with a Car subclass, a Shape class with a Rectangle subclass, an Employee class with an HRManager subclass, and a BankAccount class with a SavingsAccount subclass. Each example demonstrates the use of inheritance and polymorphism, along with expected outputs and explanations.

Uploaded by

jeevanmunna0892
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

1. Write a 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.

The above code defines a Java class named 'Main' with a 'main' method. Inside the 'main'
method:

An 'Animal' object named 'animal' is created.

A 'Cat' object named 'cat' is created.


Both 'Animal' and 'Cat' are classes defined elsewhere in the code. They have a method called
'makeSound()'.

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:

The animal makes a sound.


The cat quarrels.
2. Write a 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".
In this exercise above -

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:

The area of the rectangle is: 30.0

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

Adding new employee!

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.

The SavingsAccount class is a subclass of BankAccount and overrides the withdraw()


method. It checks if the account balance falls below one hundred before allowing a
withdrawal. The method prints an error message if the balance is below one hundred. If the
balance is greater than or equal to one hundred, the method calls the withdraw() method of
the superclass to withdraw.

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:

You might also like