0% found this document useful (0 votes)
21 views5 pages

Lab5 Excercise

The document outlines a series of exercises focused on Java inheritance concepts, including single, hierarchical, and multilevel inheritance, as well as method overriding, overloading, and interface implementation. Each exercise provides a brief description, sample input, and expected output to guide the implementation of various classes and methods. The exercises cover a range of scenarios such as employee payroll, banking systems, and media playback.
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)
21 views5 pages

Lab5 Excercise

The document outlines a series of exercises focused on Java inheritance concepts, including single, hierarchical, and multilevel inheritance, as well as method overriding, overloading, and interface implementation. Each exercise provides a brief description, sample input, and expected output to guide the implementation of various classes and methods. The exercises cover a range of scenarios such as employee payroll, banking systems, and media playback.
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/ 5

Exercise 1: Implement Single Inheritance

Write a Java program where a Person class has attributes name and age. A subclass Employee
extends Person and adds salary. Display the details.

Sample Input Sample Output

Enter name: Alice Name: Alice


Enter age: 25 Age: 25
Enter salary: 50000 Salary: 50000

Exercise 2: Implement Hierarchical Inheritance

Create a base class Vehicle with a method displayType(). Extend it with Car and Bike classes and
override the method to display respective types.

Sample Input Sample Output

Choose vehicle type (1 for Car, 2 for Bike): 1 Vehicle Type: Car

Choose vehicle type (1 for Car, 2 for Bike): 2 Vehicle Type: Bike

Exercise 3: Multilevel Inheritance - Animal Hierarchy

Create a base class Animal with a method makeSound(). Extend it into Mammal, and further
extend Dog from Mammal. Implement the makeSound() method in each class and display the
hierarchy.

Sample Input Sample Output

Animal makes a sound


Creating a Dog object Mammal gives birth
Dog says: Woof!

Exercise 4: Multilevel Inheritance - Vehicle System

Create a Vehicle class with a method move(). Extend it into Car, and further extend ElectricCar
from Car. Implement methods to describe movement at each level.
Sample Input Sample Output

Vehicle moves
Creating an ElectricCar object Car drives
Electric Car moves silently

Exercise 5: Multilevel Inheritance - University System

Create a Person class with attributes name and age. Extend it into Student, adding id, and
further extend it into GraduateStudent, adding thesisTitle. Display details at each level.

Sample Input Sample Output

Enter name: Alice Name: Alice


Enter age: 23 Age: 23
Enter ID: 251-15-123 ID: 251-15-123
Enter thesis title: AI in Healthcare Thesis Title: AI in Healthcare

Exercise 6: Multilevel Inheritance - Employee Payroll

Create an Employee class with name and salary. Extend it into Manager, adding bonus, and
further extend into SeniorManager, adding stockOptions. Calculate total earnings.

Sample Input Sample Output

Name: Bob
Enter Name: Bob
Base Salary: 50000
Enter Salary: 50000
Bonus: 10000
Enter Bonus: 10000
Stock Options: 20000
Enter Stock Options: 20000
Total Earnings: 80000

Exercise 7: Multilevel Inheritance - Banking System

Create a Bank class with bankName. Extend it into Branch, adding branchCode, and further
extend into Account, adding accountHolder and balance. Display account details.

Sample Input Sample Output

Enter Bank Name: XYZ Bank Bank: XYZ Bank


Enter Branch Code: 101 Branch Code: 101
Sample Input Sample Output

Enter Account Holder: David Account Holder: David


Enter Balance: 5000 Balance: 5000

Exercise 8: Method Overriding

Create a base class BankAccount with a method getInterestRate(). Extend it into SavingsAccount
and CurrentAccount, overriding the method to return different interest rates.

Sample Input Sample Output

Choose Account Type (1 for Savings, 2 for


Interest Rate: 5%
Current): 1

Choose Account Type (1 for Savings, 2 for


Interest Rate: 2%
Current): 2

Exercise 9: Method Overloading (Compile-time Polymorphism)

Write a program to calculate the area of a square and rectangle using method overloading.

Sample Input Sample Output

Enter side of square: 4 Area of Square: 16

Enter length: 5
Area of Rectangle: 15
Enter width: 3

Exercise 10: Runtime Polymorphism

Create a Shape class with a method draw(). Extend it into Circle and Rectangle, overriding
draw() to print different shapes.

Sample Input Sample Output

Choose Shape (1 for Circle, 2 for Rectangle): 1 Drawing a Circle

Choose Shape (1 for Circle, 2 for Rectangle): 2 Drawing a Rectangle


Exercise 11: Abstract Class

Create an abstract class Animal with an abstract method makeSound(). Implement it in Dog and
Cat classes.

Sample Input Sample Output

Choose Animal (1 for Dog, 2 for Cat): 1 Dog says: Woof!

Choose Animal (1 for Dog, 2 for Cat): 2 Cat says: Meow!

Exercise 12: Use of Final Keyword

Create a final class MathUtils with a method square(). Try to inherit it and observe the compiler
error.

Sample Input Sample Output

Enter a number: 5 Square of 5: 25

Exercise 13: Upcasting and Downcasting

Create a Parent class with a method show(). Extend it into a Child class. Demonstrate upcasting
(Parent p = new Child();) and downcasting (Child c = (Child) p;).

Sample Input Sample Output

Using upcasting Parent method called

Using downcasting Child method called

Exercise 14: Interface Implementation

Create an interface Playable with a method play(). Implement it in Music and Video classes.

Sample Input Sample Output

Choose media type (1 for Music, 2 for


Playing music...
Video): 1
Sample Input Sample Output

Choose media type (1 for Music, 2 for


Playing video...
Video): 2

You might also like