OOP Basic Concepts in Java

Object-Oriented Programming (OOP) is a programming paradigm centered around the concept of objects, which can contain data, in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).

OOP is essential for several reasons:

  • It helps organize complex code through the use of classes and objects.
  • Facilitates code reusability through inheritance.
  • Enables the implementation of real-world entities in programming.
  • Improves code maintenance and flexibility.

Basic Concepts of OOP

1. Encapsulation

Encapsulation is the mechanism of bundling the data (attributes) and code acting on the data (methods) together as a single unit. It also restricts access to some of the object’s components, which is why encapsulation is also known as data hiding.

Why it’s needed: Encapsulation helps to protect the internal state of an object from unwanted external modification and bundles the data with methods that operate on that data.

2. Inheritance

Inheritance is a mechanism where a new class is derived from an existing class. The new class is called a subclass, and the existing class is called a superclass. Inheritance represents the IS-A relationship.

Why it’s needed: It promotes code reusability, allows for the logical grouping of classes, and enables polymorphism.

3. Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common super class. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

Why it’s needed: It enables one interface to be used for a general class of actions, which helps in reducing complexity by allowing the same interface to be used for different underlying forms (data types).

4. Abstraction

Abstraction is the concept of hiding the complex reality while exposing only the necessary parts. It’s about making complex systems simple by hiding the unnecessary details from the user.

Why it’s needed: It reduces complexity and allows the programmer to focus on interactions at a higher level.

Basic Concepts of OOP Illustrated with a Library Management System

Encapsulation: Book Class

Encapsulation is demonstrated here by keeping title and author private and accessible only through a constructor and a public method. Using getters and setters not only ensures that the internal state of these objects is protected and accessible only through these methods but also allows for the possibility of validation and preprocessing before setting a value.

Inheritance: Extending Book Class

AudioBook Class


Inheritance allows EBook to inherit from Book and extend its functionality.

Polymorphism: Method Overriding

The displayInfo method in EBook and potentially other subclasses (like AudioBook) can override the displayInfo method in Book, providing specific implementations. This is polymorphism in action, where the same method behaves differently based on the object that invokes it.


While the original examples did not explicitly include an abstract class or interface to demonstrate abstraction, let’s conceptualize how it might be applied:

An AbstractBook class or Readable interface could declare a method like displayInfo() without providing an implementation. Subclasses would then provide their specific implementations. This way, abstraction hides the complex details behind a simpler interface.

The provided code snippets for Book, EBook, and potentially AudioBook classes illustrate encapsulation (protecting the state of an object), inheritance (creating a new class from an existing class), and polymorphism (methods behaving differently based on the object type). Abstraction could further enhance this model by defining a common interface or abstract class for books, ensuring even greater flexibility and decoupling.

Demonstrating the use of all book types

To demonstrate the use of all book types (Book, EBook, and AudioBook) in a unified context, let’s create a LibraryDemo class. This class will instantiate objects of each book type and showcase how each behaves according to its specific implementation, while still adhering to the principles of OOP (Object-Oriented Programming).

The LibraryDemo Class

Expected Output and Explanation

In this demonstration:

  • We first instantiate and display information for a regular Book, an EBook, and an AudioBook. Each book type utilizes the displayInfo method to present details relevant to its type, illustrating polymorphism—the same method behaves differently depending on the object.
  • After initial display, we update the EBook file size and the AudioBook duration using setters, showcasing encapsulation—internal state changes through public methods ensure data integrity and encapsulation.
  • The EBook and AudioBook classes inherit from the Book class, reusing its properties and displayInfo method. They also override the displayInfo method to add additional details specific to each type, further demonstrating polymorphism.

This example solidly illustrates the practical application of encapsulation, inheritance, and polymorphism—the core principles of OOP—in a Java program.

Leave a Reply

Your email address will not be published. Required fields are marked *