0% found this document useful (0 votes)
11 views10 pages

Oops

This case study explores inheritance in Object-Oriented Programming (OOP) through a Library Management System that manages various media types like books, DVDs, and eBooks. It demonstrates how inheritance promotes code reusability and maintainability by allowing derived classes to inherit common properties and methods from a base class. The study also highlights potential challenges of inheritance, such as tight coupling and deep inheritance hierarchies.

Uploaded by

Sachin Pandey
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)
11 views10 pages

Oops

This case study explores inheritance in Object-Oriented Programming (OOP) through a Library Management System that manages various media types like books, DVDs, and eBooks. It demonstrates how inheritance promotes code reusability and maintainability by allowing derived classes to inherit common properties and methods from a base class. The study also highlights potential challenges of inheritance, such as tight coupling and deep inheritance hierarchies.

Uploaded by

Sachin Pandey
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
You are on page 1/ 10

Case Study: Inheritance in Object-Oriented Programming

Introduction
Inheritance is one of the key features of Object-Oriented Programming (OOP),
allowing classes to inherit properties and behaviours (methods) from other
classes. It enables the creation of hierarchical relationships between classes,
promoting code reusability and reducing redundancy. In this case study, we will
explore how inheritance works in an OOP context through a real-world example
of a library management system.
This system will involve the creation of various types of media (like books, DVDs,
and eBooks) and demonstrate how inheritance can be leveraged to reduce code
duplication, enhance maintainability, and make the system extensible for future
requirements.

Case Background
Imagine a Library Management System (LMS) for a medium-sized library that
handles both physical books and digital media, such as DVDs and eBooks. The
library needs to store information about the media, check items in and out, and
track their availability. The system is expected to be scalable to support
additional types of media in the future.
Key Classes in the System
• Media (Base Class): A general representation of any media item in the
library (whether it's a book, DVD, or eBook). It will contain properties and
methods that are common to all types of media.
• Book (Derived Class): A specific type of media that represents physical
books. It will inherit from Media and add additional properties and
behaviours specific to books.
• DVD (Derived Class): A specific type of media representing DVDs,
inheriting from Media and adding unique properties and methods
relevant to DVDs.
• eBook (Derived Class): A specific type of media representing digital
books, inheriting from Media and incorporating additional behaviors
relevant to eBooks.
By using inheritance, the goal is to create a structure that allows for efficient
code reuse and easy extension when adding new media types (e.g., audiobooks
or magazines).
Inheritance in Action
Step 1: Base Class Definition (Media)
The Media class represents the common properties and methods shared by all
types of media. This might include things like a title, author, and status
(whether the item is checked in or checked out).
Source code
public class Media {

private String title;

private String creator;

private String status;

// Constructor

public Media(String title, String creator, String status) {

this. title = title;

this.creator = creator;

this. status = (status! = null)? status: "available”; // Default to "available" if status is null

// Default constructor that assumes status is "available"

public Media (String title, String creator) {

this (title, creator, "available");

// Method to check out the media

public void checkOut () {

if (status. equals("available")) {

status = "checked out";

System.out.println(title + " has been checked out.");


} else {

System.out.println(title + " is not available for checkout.");

// Method to check in the media

public void checkIn() {

status = "available";

System.out.println(title + " has been checked in.");

// Method to print media details

@Override

public String toString() {

return title + " by " + creator + " (Status: " + status + ")";

// Getter methods (optional, for accessing fields if needed)

public String getTitle() {

return title;

public String getCreator() {

return creator;

public String getStatus() {

return status;

}
Step 2: Derived Class Definition (Book)
The Book class inherits from Media and adds additional properties such as the
book's genre and its page count. It can also define specific behaviours unique to
books, like checking whether a book is overdue or adding a genre attribute.

Source code
public class Book extends Media {

private int pageCount;

private String genre;

// Constructor

public Book(String title, String author, int pageCount, String genre, String status) {

super(title, author, status); // Call to the parent constructor

this.pageCount = pageCount;

this.genre = genre;

// Overloaded constructor with default status "available"

public Book(String title, String author, int pageCount, String genre) {

super(title, author); // Calls the Media constructor with default status "available"

this.pageCount = pageCount;

this.genre = genre;

// Method to check if the book is overdue

public void overdue(int daysLate) {

if (daysLate > 0) {

System.out.println(getTitle() + " is " + daysLate + " days overdue!");

} else {

System.out.println(getTitle() + " is not overdue.");


}

// Override the toString() method to provide a custom string representation

@Override

public String toString() {

return "Book: " + getTitle() + " by " + getCreator() + ", Genre: " + genre

+ ", Pages: " + pageCount + " (Status: " + getStatus() + ")";

// Getter methods for pageCount and genre

public int getPageCount() {

return pageCount;

public String getGenre() {

return genre;

Step 3: Derived Class Definition (DVD)


The DVD class also inherits from Media, adding properties and methods specific
to DVDs, like duration (runtime in minutes) and region code (for DVD region
restrictions).
Source code

public class DVD extends Media {


private int duration; // Duration of the DVD in minutes
private String regionCode; // Region code for the DVD

// Constructor
public DVD(String title, String director, int duration, String regionCode, String status) {
super(title, director, status); // Call to the parent constructor
this.duration = duration;
this.regionCode = regionCode;
}

// Overloaded constructor with default status "available"


public DVD(String title, String director, int duration, String regionCode) {
super(title, director); // Calls the Media constructor with default status "available"
this.duration = duration;
this.regionCode = regionCode;
}

// Override the toString() method to provide a custom string representation


@Override
public String toString() {
return "DVD: " + getTitle() + ", Director: " + getCreator() + ", Duration: " + duration
+ " mins, Region: " + regionCode + " (Status: " + getStatus() + ")";
}

// Getter methods for duration and regionCode


public int getDuration() {
return duration;
}

public String getRegionCode() {


return regionCode;
}
}
Step 4: Derived Class Definition (eBook)
The eBook class, like Book and DVD, inherits from Media. However, it can also
add properties specific to eBooks, such as file size and format (e.g., EPUB, PDF).
Source code

public class eBook extends Media {


private int fileSize; // File size of the eBook in MB
private String format; // Format of the eBook (e.g., PDF, EPUB, MOBI)

// Constructor
public eBook(String title, String author, int fileSize, String format, String status) {
super(title, author, status); // Call to the parent constructor
this.fileSize = fileSize;
this.format = format;
}

// Overloaded constructor with default status "available"


public eBook(String title, String author, int fileSize, String format) {
super(title, author); // Calls the Media constructor with default status "available"
this.fileSize = fileSize;
this.format = format;
}

// Override the toString() method to provide a custom string representation


@Override
public String toString() {
return "eBook: " + getTitle() + " by " + getCreator() + ", Format: " + format
+ ", Size: " + fileSize + "MB (Status: " + getStatus() + ")";
}

// Getter methods for fileSize and format


public int getFileSize() {
return fileSize;
}

public String getFormat() {


return format;
}}

Step 5: Polymorphism in Action


One of the key advantages of inheritance in OOP is polymorphism,
where you can treat objects of different derived classes as objects of
the base class (Media), allowing for dynamic method binding.
Source code

public class Main {


public static void main(String[] args) {
// Create instances of each class
Book book1 = new Book("To Kill a Mockingbird", "Harper Lee", 281,
"Fiction");
DVD dvd1 = new DVD("Inception", "Christopher Nolan", 148, "Region 1");
eBook ebook1 = new eBook("Digital Fortress", "Dan Brown", 1, "EPUB");

// Create a list of media items


Media[] mediaList = {book1, dvd1, ebook1};

// Display information of different media types and check them out


for (Media media : mediaList) {
displayMediaInfo(media);
media.checkOut(); // Polymorphism: Calls checkOut() on the correct
object type
}
}

// Method to display media information


public static void displayMediaInfo(Media mediaItem) {
System.out.println(mediaItem); // Calls the appropriate toString() method
depending on the object's type
}
}

Challenges and Considerations


While inheritance offers many benefits, it also comes with potential
challenges:
1. Tight Coupling: Subclasses are tightly coupled to their parent
class, meaning changes in the parent class can potentially affect
all derived classes. This requires careful design to avoid
unnecessary dependencies.
2. Inheritance Hierarchy Issues: Deep inheritance hierarchies can
become difficult to manage and understand. Overuse of
inheritance may lead to code that's harder to maintain or
extend.
3. Overriding vs. Extending: When subclassing, it's important to
decide whether to override or extend parent methods.
Overriding may lead to different behaviours than expected,
especially when polymorphism is used.
Conclusion
Inheritance in OOP is a powerful concept that promotes code reuse,
modularity, and flexibility. Through the example of a Library
Management System, we demonstrated how inheritance can be
applied to different types of media—books, DVDs, and eBooks—
allowing the system to handle various media types efficiently. By
using inheritance, the system can easily accommodate new types of
media in the future, such as audiobooks or magazines, by simply
adding new derived classes. However, careful design is necessary to
avoid common pitfalls such as deep inheritance hierarchies and tight
coupling.

You might also like