◆ Study Hub By J

Study Hub › Study Guides

PROGRAMMING · OOP

Object-Oriented Programming in Java: The Four Pillars Explained Simply

By Janidu Sandaruwan · Updated October 2026 · 3 min read

Object-oriented programming (OOP) is how most real software is organised, and it is the core of second-semester programming modules. The ideas are simple once you see them in code. This guide explains each one with a small Java example you can type and run.

1. Classes and objects

A class is a blueprint: it describes what data something has (fields) and what it can do (methods). An object is one real thing built from that blueprint. One Student class can create thousands of student objects, each with its own name and marks.

public class Student {
    private String name;
    private int marks;

    public Student(String name, int marks) {   // constructor
        this.name = name;
        this.marks = marks;
    }

    public boolean hasPassed() {
        return marks >= 40;
    }
}

Student s1 = new Student("Nimal", 72);          // an object

2. Encapsulation

Encapsulation means keeping an object's data private and allowing access only through methods. This protects the data from invalid changes. In the class above, marks is private, so no other class can set it to −50 directly. If we allow changes, we do it through a method that checks the value:

public void setMarks(int marks) {
    if (marks >= 0 && marks <= 100) {
        this.marks = marks;
    }
}
public int getMarks() { return marks; }

These are called getters and setters. The key exam point: encapsulation is not just "making fields private", it's controlling how data can change.

3. Inheritance

Inheritance lets a new class reuse and extend an existing one using extends. The new class (subclass) gets the fields and methods of the parent (superclass) and can add its own. It models an "is-a" relationship: a PostgradStudent is a Student.

public class PostgradStudent extends Student {
    private String supervisor;

    public PostgradStudent(String name, int marks, String supervisor) {
        super(name, marks);          // call the parent constructor
        this.supervisor = supervisor;
    }
}

Java allows a class to extend only one class (single inheritance), but it can implement many interfaces.

4. Polymorphism

Polymorphism means "many forms": the same method call can behave differently depending on the actual object. There are two kinds you need to know:

class Shape { double area() { return 0; } }
class Circle extends Shape {
    double r;
    Circle(double r) { this.r = r; }
    @Override double area() { return Math.PI * r * r; }
}

Shape s = new Circle(2);   // parent reference, child object
System.out.println(s.area());   // runs Circle's area(): 12.566...

5. Abstraction: abstract classes and interfaces

Abstraction means showing what an object does while hiding how it does it. Java gives two tools:

Abstract classInterface
Keywordabstract class, used with extendsinterface, used with implements
Can have constructors and fields with stateYesNo (only constants)
How many can a class use?OneMany
Best forClosely related classes sharing codeA capability many unrelated classes can have

An abstract class cannot be instantiated with new. Any class that extends it must implement its abstract methods, or be abstract itself.

6. Common exam mistakes

J

Written by Janidu Sandaruwan, an IT undergraduate at SLIIT and the creator of Study Hub By J. Guides are written from a student's point of view; always confirm official rules with your faculty and module outline.

Study Hub By J: a non-profit student help platform, made for university students. 💙
Study Guides · Study Hub · GPA Calculator · Privacy & Policy · About the creator
Independent student project · not officially affiliated with any university · © 2026 Study Hub By J