0% found this document useful (0 votes)
3 views4 pages

w4 Abstract Interface All Java

The document outlines a series of exercises and real-world scenarios for implementing object-oriented programming (OOP) concepts such as inheritance, polymorphism, abstraction, and interfaces. It includes both intermediate and advanced exercises focusing on class design, method overriding, exception handling, and generics, as well as practical applications in various systems like e-commerce, banking, and healthcare. Each scenario illustrates how OOP principles can enhance code reusability and system organization.
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)
3 views4 pages

w4 Abstract Interface All Java

The document outlines a series of exercises and real-world scenarios for implementing object-oriented programming (OOP) concepts such as inheritance, polymorphism, abstraction, and interfaces. It includes both intermediate and advanced exercises focusing on class design, method overriding, exception handling, and generics, as well as practical applications in various systems like e-commerce, banking, and healthcare. Each scenario illustrates how OOP principles can enhance code reusability and system organization.
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

Note: Students are required to implement all necessary properties and methods for each class, as

well as a main function to demonstrate the correctness of their code.

Intermediate Exercises

1. Inheritance
o Create a Vehicle superclass and derive Car and Bike subclasses. Add properties
and methods specific to each subclass and demonstrate inheritance.
2. Method Overriding
o Extend the Vehicle example above, overriding a method in the Car and Bike
classes to show unique behavior when starting the vehicle.
3. Super Keyword
o Add constructors to the Car and Bike classes that call the Vehicle superclass
constructor using super.
4. Abstract Class
o Define an abstract class Animal with an abstract method sound(). Create Dog and
Cat subclasses that implement the sound() method.
5. Polymorphism with Abstract Classes
o Create a Shape abstract class with an abstract method calculateArea().
Implement Circle and Rectangle classes that override calculateArea().

Advanced Exercises

6. Interface Implementation
o Create an Animal interface with methods like eat() and sleep(). Implement the
interface in classes like Dog, Cat, and Bird.
7. Multiple Interfaces
o Design two interfaces, Flyable and Swimmable, and create a class Duck that
implements both interfaces with unique behaviors.
8. Polymorphism with Interfaces
o Write a program with an interface Playable and implement it in Football and
Cricket classes. Demonstrate polymorphic behavior by using Playable
references.
9. Composition
o Create a Library class that contains multiple Book objects. Show how one class
can contain instances of another class.
10. Aggregation
o Define a Company class that has a list of Employee objects. Demonstrate
aggregation by creating multiple employees and associating them with a company.
11. Inner Classes
o Create an Outer class with an inner class Inner. Show how to instantiate the inner
class and access its properties/methods.
12. Exception Handling in OOP
o Create a BankAccount class with methods to deposit and withdraw. Throw
custom exceptions for scenarios like InsufficientFundsException.
13. Object Cloning
o Implement a Student class and create a method to clone objects using Cloneable
interface. Show how changes to the original object do not affect the clone.
14. Generics with Classes and Methods
o Create a generic class Box that can hold any type of object, and implement methods
to add, remove, and display items in the box.
15. File Handling with OOP Concepts
o Create a FileManager class with methods for reading from and writing to files.
Use encapsulation to hide the file operations and handle exceptions properly.

Real word scenarios: these scenarios demonstrate how inheritance structures and OOP principles
can organize complex systems and promote code reusability, making development more
manageable and scalable.

1. E-commerce Platform

• Scenario: You’re building an e-commerce platform where various types of products are
sold, including electronics, clothing, and books. Each product type has some unique
features (e.g., size for clothing, author for books) but also shares common attributes (like
price and description).
• OOP Application:
o Abstraction: Define an abstract class Product with properties like price, name,
and description as well as methods like calculateDiscount and
displayDetails.
o Inheritance: Create subclasses like Electronics, Clothing, and Books that
inherit from Product and add their unique attributes and behaviors.
o Polymorphism: Use polymorphism by creating a common interface for calculating
discounts across different product types, enabling each subclass to implement
discounts differently.
o Interface: Define interfaces for shipping methods, like Shipable for standard
shipping, and DigitalDelivery for instant downloads. Each product can
implement the interface(s) it needs.

2. Banking System

• Scenario: Developing a banking system that includes multiple account types, such as
savings, checking, and credit accounts. Each account type has different characteristics and
transaction rules.
• OOP Application:
o Abstraction: Create an abstract class Account that includes common properties
(e.g., balance, accountNumber) and methods like deposit, withdraw, and
calculateInterest.
o Inheritance: Define specific account types, like SavingsAccount,
CheckingAccount, and CreditAccount, inheriting from the Account class and
implementing account-specific rules.
o Polymorphism: Enable polymorphic behavior by allowing different account types
to override the calculateInterest and withdraw methods based on the account
rules.
o Interface: Create interfaces like Loanable or InterestBearing that specify
methods for applying loans or calculating interest rates, allowing specific account
types to implement them as needed.

3. School Management System

• Scenario: You’re building a system for a school where there are students, teachers, and
administrative staff. Each group has its own properties and responsibilities, but they also
share some common characteristics.
• OOP Application:
o Abstraction: Define an abstract class Person with properties like name, age, and
ID and common methods like login and viewProfile.
o Inheritance: Create subclasses like Student, Teacher, and Staff that inherit from
Person and implement group-specific methods and properties.
o Polymorphism: Implement polymorphic behavior for a schedule method so each
subclass can display its schedule differently (e.g., classes for students, teaching
hours for teachers, and duty hours for staff).
o Interface: Define interfaces such as Graded for grading capabilities, which only
Teacher would implement, or Registrable for enrollment capabilities for
students.

4. Home Automation System

• Scenario: Developing a software system for managing various smart devices (e.g., lights,
thermostats, security cameras, etc.) within a home automation network.
• Classes: Define a base class Device with attributes like status, power, and location.
Derived classes could be Light, Thermostat, and Camera.
• Inheritance: Light, Thermostat, and Camera inherit from Device.
• Polymorphism: Each device class has its own implementation of a turnOn and turnOff
method. This allows for dynamic calls based on the device type (e.g., [Link]() vs.
[Link]()).
• Abstraction: Abstract the common functionalities like DeviceStatus or DeviceControl
for generic device management without worrying about specific device details.
• Interface: Define an interface ControllableDevice that requires any device to
implement methods like start, stop, schedule, and setTimer.

5. Healthcare Management System


• Scenario: Managing patients, doctors, appointments, and treatments in a healthcare
facility.
• Classes: A base class Person with common attributes (name, age, address). Subclasses
could be Doctor, Patient, and Nurse.
• Inheritance: Doctor and Patient inherit from Person.
• Polymorphism: A method like performDuty behaves differently based on whether it's a
Doctor or Nurse—for example, a Doctor diagnoses and prescribes, while a Nurse assists
and administers medication.
• Abstraction: Abstract out common hospital actions (e.g., HospitalManagement,
RecordKeeping) for patient, doctor, and treatment management.
• Interface: Define an interface MedicalRecordHandler with required methods for
creating, updating, and archiving medical records, implemented by classes that manage
different types of records.

6. Travel Booking System

• Scenario: Building a system for booking flights, hotels, and car rentals.
• Classes: Define a base class Booking with common attributes (bookingID, customerInfo,
price). Derived classes could be FlightBooking, HotelBooking, and
CarRentalBooking.
• Inheritance: FlightBooking, HotelBooking, and CarRentalBooking inherit from
Booking.
• Polymorphism: Each booking type has a different calculateTotalCost method,
allowing cost calculation for flight tickets, hotel nights, or car rental days.
• Abstraction: Abstract the booking process as a Bookable type, handling

You might also like