Sources

  1. GeeksforGeeks. (2025, January 21). Encapsulation in Java. https://www.geeksforgeeks.org/encapsulation-in-java/
  2. Kaur, G. (2023, October 19). The Four Pillars of OOPs (Object Oriented Programming). AlmaBetter. https://www.almabetter.com/bytes/articles/four-pillars-of-oops
  3. Parr, K. (2020, December 18). The Four Pillars of Object-Oriented Programming. freeCodeCamp.Org. https://www.freecodecamp.org/news/four-pillars-of-object-oriented-programming/
  4. Runiwal, R. (2024, June 21). Four Pillars of Object-Oriented Programming (OOPS)—Naukri Code 360. Code360. https://www.naukri.com/code360/library/four-pillars-of-oops
  5. Stroustrup, B. (1988). What is object-oriented programming? IEEE Software, 5(3), 10–20. https://doi.org/10.1109/52.2020
  6. Wegner, P. (1990). Concepts and paradigms of object-oriented programming. ACM SIGPLAN OOPS Messenger, 1(1), 7–87. https://doi.org/10.1145/382192.383004

What is Object-Oriented Programming

  • Procedural programming puts emphasis on functions to organize a number of algorithms (Stroustrup, 1988). In contrast, object-oriented programming (OOP) is characterized by its capacity to express general properties of objects. Fundamental concepts underpinning object-oriented programming include objects, classes, and inheritance.
  • Under the object-oriented paradigm, objects are composed of operations sharing a state (Wegner, 1990). These operations are referred to as the methods of a particular object, whereas the variables that signify the object’s state are referred to as the object’s instance variables.
  • Only the object’s operations can view and alter its state; this state is hidden from the outside. For this reason, the methods within the object control its interface and behavior (Wegner, 1990).
  • Another important concept in object-oriented programming are classes. Objects can be created and based on these classes (Wegner, 1990). Unlike objects, however, classes only possess potential instance variables—variables which are only instantiated when an object is created.
  • Inheritance is an efficient feature provided by classes whereby it allows subclasses to inherit their parent’s operations; nonetheless, they can still create their own unique operations and instance variables when needed (Wegner, 1990). Inheritance is useful for defining new classes that rely on reused class behavior.

Four Pillars of Object-Oriented Programming

Programming paradigms are rules that programmers adhere to when coding. In this regard, the pillars of object-oriented programming pertain to the principles developers follow to produce elegant code (Parr, 2020). There are four pillars of object-oriented programming: abstraction, encapsulation, inheritance, and polymorphism.

1. Abstraction

Abstraction is essential for enhancing the code readability, maintainability, and reusability (Runiwal, 2024). When we abstract something, we only reveal the object’s pertinent and core aspects, while hiding away their irrelevant implementation details (Kaur, 2023; Parr, 2020).

In Java, we abstract something through the abstract class. The code below provides an example implementation of it for an employee class.

// Abstract class  
abstract class Employee {  
    // Abstract method - custom implementation based on the subclass  
    public abstract void work();  
    // Regular method - subclass inherits its implementation  
    public void greet() {  
       System.out.println("Good morning!");  
    }  
}  
  
// Subclass of the Employee abstract class  
class Chef extends Employee {  
    public void work() {  
       // Custom implementation of the abstract work() method  
       System.out.println("Time to start cooking");  
    }  
}  
  
class Abstraction {  
    public static void main(String[] args) {  
       Chef cook = new Chef();  
       cook.greet();  
       cook.work();  
    }  
}

2. Encapsulation

Encapsulation focuses on the management of access to object data and methods (Kaur, 2023; Parr, 2020; Runiwal, 2024). It is crucial for data security and modularity: it safeguards data from unauthorized access and fosters a coding style composed of independent but cohesive parts. With encapsulation, we can avoid coupling code to unrelated global variables, thereby preventing bugs that emerge from reused variable names (Parr, 2020).

We can implement encapsulation in Java by creating private instance variables. Because direct access is restricted, we use public getter methods to share variable values, and public setter methods to modify them (GeeksforGeeks, 2025).

The example code below demonstrates how encapsulation can be carried out in Java.

class Student {  
    // Private instance variables  
    private int id;  
    private String name;  
  
    // Getter Methods  
    public int getId() {return id;}  
    public String getName() {return name;}  
  
    // Setter Methods  
    public void setId(int id) {this.id = id;}  
    public void setName(String name) {this.name = name;}  
}  
  
public class Encapsulation {  
    public static void main(String[] args) {  
        Student s = new Student();  
  
        // Modifying object instance variables through setter methods  
        s.setId(1048596);  
        s.setName("Barrel Titor");  
  
        // Retrieving object instance variables through getter methods  
        System.out.println("Student ID: "+ s.getId());  
        System.out.println("Student Name: "+ s.getName());  
    }  
  
}

3. Inheritance

Inheritance involves a subclass inheriting the properties and methods of their superclass (Kaur, 2023; Parr, 2020; Runiwal, 2024). As a result, it reduces redundancy and allows for reusable code. It also makes it easier to make changes to multiple classes—changes to a superclass property or method are mirrored by their subclasses (Kaur, 2023).

We apply inheritance in Java through extends, as illustrated in the example code below:

class Machine {  
  
    // The 'protected' access modifier makes the attribute accessible for subclasses  
    protected String brand = "IBM";  
  
    // Method to be inherited by the subclass  
    public String getBrand(){  
        return brand;  
    }  
}  
 
// Use `extends` to inherit attributes and methods from the Machine class
class Computer extends Machine {  
    private String model = "5100";  
  
    public String getModel(){  
        return model;  
    }  
}  
  
public class Inheritance {  
    public static void main(String[] args) {  
        Computer c = new Computer();  
        System.out.println("Computer Brand: " + c.getBrand() + "\n" + "Computer Model: " + c.getModel());  
    }  
}

4. Polymorphism

Polymorphism is defined as the condition of having a variety of forms (Parr, 2020). In the context of object-oriented programming, this principle allows subclasses to replace their parent’s implementation of a specific method. As a consequence, they enhance the versatility of subclasses (Kaur, 2023).

The example code below shows how polymorphism can be applied in Java.

class Food {
    // Base method that can be inherited or overridden by subclasses
    public void prepare(){
        System.out.println("Frying food in the frying pan");
    }
}
 
class Pasta extends Food {
    // Override the prepare() implementation of the Food class 
    public void prepare(){
        System.out.println("Boiling pasta in the saucepan");
    }
}
 
class Cake extends Food {
    // Override the prepare() implementation of the Food class 
    public void prepare(){
        System.out.println("Baking cake in the oven");
    }
}
 
public class Polymorphism {
    public static void main(String[] args){
        // Create three objects with varying classes and prepare() implementation
        Food fish = new Food();
        Pasta spaghetti = new Pasta();
        Cake cheesecake = new Cake();
 
        fish.prepare();
        spaghetti.prepare();
        cheesecake.prepare();
    }
}