0% found this document useful (0 votes)
10 views6 pages

Java Inheritance and Polymorphism Exercises

The document outlines several programming problems to be solved using Java, focusing on different inheritance types and object-oriented principles. It includes the creation of a Delivery Application with single level inheritance, a Library Management System with multi-level inheritance, a Hospital Management System with hierarchical inheritance, a Bank Management System demonstrating polymorphism, and an ATM Machine system using abstraction. Each problem specifies class structures, methods, and constraints, with sample inputs and outputs provided for clarity.
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)
10 views6 pages

Java Inheritance and Polymorphism Exercises

The document outlines several programming problems to be solved using Java, focusing on different inheritance types and object-oriented principles. It includes the creation of a Delivery Application with single level inheritance, a Library Management System with multi-level inheritance, a Hospital Management System with hierarchical inheritance, a Bank Management System demonstrating polymorphism, and an ATM Machine system using abstraction. Each problem specifies class structures, methods, and constraints, with sample inputs and outputs provided for clarity.
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

DEPARTMENT OF TRAINING AND PLACEMENT

Object Oriented Programmin Practice


Problems
1. Problem Statement:
Create a simple Delivery Application using Single Level Inheritance in Java.

Requirements:
Parent Class: User
• Create a class named User
• Data Members:
• userId (int)
• userName (String)
• Methods:
• setUserDetails(int userId, String userName)
o Assign values to userId and userName
• displayUserDetails()
o Display user ID and user name

Child Class: DeliveryPartner


• Create a class named DeliveryPartner that extends User
• Data Members:
• deliveryArea (String)
• deliveryFee (double)
Methods:
• setDeliveryDetails(String deliveryArea, double deliveryFee)
o Assign delivery area and fee
• displayDeliveryDetails()
o Display delivery area and delivery fee

Main Class:
• Create a Main class
• Create an object of DeliveryPartner
• Set user details and delivery details
• Display all information using the child class object

Constraints:
• Use only Single Level Inheritance
• Use the extends keyword
• Do not use interfaces or multiple inheritance
• No user input required (values can be hardcoded)

Sample Input: Sample Output:


User ID: 501 User ID: 501
User Name: Arjun User Name: Arjun
Delivery Area: Madhapur Delivery Area: Madhapur
Delivery Fee: 60.0 Delivery Fee: 60.0
2. Problem Statement:
Design a simple Library Management System using Multi Level Inheritance.

Base Class: Library


• Create a class named Library
• Data Members:
o libraryName (String)
• Methods:
o setLibraryName(String libraryName)
▪ Used to assign the library name
o displayLibraryName()
▪ Displays the library name

Intermediate Class: Section


• Create a class named Section that extends Library
• Data Members:
o sectionName (String)
• Methods:
o setSectionName(String sectionName)
▪ Used to assign section name
o displaySectionName()
▪ Displays the section name

Child Class: Book


• Create a class named Book that extends Section
o Data Members:
o bookTitle (String)
o authorName (String)
• Methods:
o setBookDetails(String bookTitle, String authorName)
▪ Used to assign book details
o displayBookDetails()
▪ Displays book title and author name

Main Class:
• Create a Main class
• Create an object of Book
• Set library name, section name, and book details
• Display all information using the child class object

Constraints:
• Use only Multi Level Inheritance
• Use the extends keyword correctly
• Do not use interfaces or multiple inheritance
• Values can be hardcoded (no user input)

Sample Input: Sample Output:


Library Name: Central Library Library Name: Central Library
Section Name: Computer Science Section Name: Computer Science
Book Title: Data Structures Book Title: Data Structures
Author Name: Mark Allen Author Name: Mark Allen
3. Problem Statement:
Design a simple Hospital Management System using Hierarchical Inheritance.

Parent Class: Hospital


• Create a class named Hospital
• Data Members:
o hospitalName (String)
• Methods:
o setHospitalName(String hospitalName)
▪ Used to assign the hospital name
o displayHospitalName()
▪ Displays the hospital name

Child Class 1: Doctor


• Create a class named Doctor that extends Hospital
• Data Members:
o doctorName (String)
o specialization (String)
• Methods:
o setDoctorDetails(String doctorName, String specialization)
▪ Used to assign doctor details
o displayDoctorDetails()
▪ Displays doctor name and specialization

Child Class 2: Patient


• Create a class named Patient that extends Hospital
• Data Members:
o patientName (String)
o disease (String)
• Methods:
o setPatientDetails(String patientName, String disease)
▪ Used to assign patient details
o displayPatientDetails()
▪ Displays patient name and disease

Main Class:
• Create a Main class
• Create an object of Doctor
• Create an object of Patient
• Set hospital name for both objects
• Set doctor details and patient details
• Display all details using respective objects

Constraints:
• Use only Hierarchical Inheritance
• Multiple child classes must inherit from one parent class
• Use the extends keyword
• Do not use interfaces or multiple inheritance
• Values can be hardcoded (no user input)
Sample Input:
Hospital Name: City Care Hospital
Doctor Name: Dr. Meena
Specialization: Cardiology
Patient Name: Ramesh
Disease: Heart Problem

Sample Output:
Hospital Name: City Care Hospital
Doctor Name: Dr. Meena
Specialization: Cardiology
Hospital Name: City Care Hospital
Patient Name: Ramesh
Disease: Heart Problem

4. Problem Statement:
Design a simple Bank Management System using Polymorphism and Parameterized Constructors
in Java.

Parent Class: BankAccount


• Create a class named BankAccount
• Parameterized Constructor:
o BankAccount(String accountHolder, double balance)
o Assigns accountHolder and balance
o Displays a message indicating the bank account is created with account holder
name and balance
• Methods:
o calculateInterest()
▪ Displays a generic interest calculation message

Child Class 1: SavingsAccount


• Create a class named SavingsAccount that extends BankAccount
• Parameterized Constructor:
o SavingsAccount(String accountHolder, double balance)
o Calls parent class constructor
o Displays a message indicating a savings account is created
• Methods:
o Override calculateInterest()
▪ Displays interest calculation for savings account (e.g., 4% of balance)

Child Class 2: CurrentAccount


• Create a class named CurrentAccount that extends BankAccount
• Parameterized Constructor:
o CurrentAccount(String accountHolder, double balance)
o Calls parent class constructor
o Displays a message indicating a current account is created
• Methods:
o Override calculateInterest()
o Displays interest calculation for current account (e.g., 0% of balance)
Main Class:
• Create a Main class
• Create a reference variable of BankAccount
• Assign a SavingsAccount object (with name and balance) to BankAccount reference
• Call calculateInterest()
• Assign a CurrentAccount object (with name and balance) to BankAccount reference
• Call calculateInterest()

Constraints:
• Use method overriding
• Demonstrate runtime polymorphism
• Use parameterized constructors in parent and child classes
• Use parent class reference to call child class methods
• Do not use interfaces
• Values can be hardcoded (no user input)

Sample Input:
Savings Account: Name = Arjun, Balance = 5000
Current Account: Name = Ramesh, Balance = 10000

Sample Output:
Bank Account Created for Arjun with balance 5000.0
Savings Account Created
Interest for Savings Account: 200.0
Bank Account Created for Ramesh with balance 10000.0
Current Account Created
Interest for Current Account: 0.0

5. Problem Statement:
Design a simple ATM Machine system using Abstraction in Java.

Abstract Class: ATM


• Create an abstract class named ATM
• Abstract Methods:
o withdraw(double amount)
▪ Used to withdraw money
o deposit(double amount)
▪ Used to deposit money
• Concrete Methods (optional):
o checkBalance(double balance)
▪ Display the current account balance

Child Class: BankATM


• Create a class named BankATM that extends ATM
• Data Members:
o balance (double)
• Constructor:
o BankATM(double initialBalance)
▪ Initializes the balance
• Implement Abstract Methods:
o withdraw(double amount)
▪ Deducts amount from balance if sufficient, otherwise shows error
o deposit(double amount)
▪ Adds amount to balance
o Can also use checkBalance() method from parent

Main Class:
• Create a Main class
• Create an object of BankATM using ATM reference
• Call withdraw, deposit, and checkBalance methods
• Demonstrate how abstraction hides internal implementation

Constraints:
• Use abstract class ATM
• Implement all abstract methods in child class
• Demonstrate runtime abstraction
• No user input required (values can be hardcoded)
• Do not use interfaces for this example

Sample Input:
Initial Balance: 5000
Withdraw: 1000
Deposit: 2000

Sample Output:
Current Balance: 5000.0
1000.0 withdrawn successfully.
2000.0 deposited successfully.
Current Balance: 6000.0

You might also like