Object-Oriented Programming in Java: The Four Pillars Explained Simply
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:
- Overriding (runtime polymorphism): a subclass provides its own version of a parent method with the same signature. Java decides which version to run at runtime, based on the real object type.
- Overloading (compile-time polymorphism): several methods in the same class share a name but have different parameter lists.
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 class | Interface | |
|---|---|---|
| Keyword | abstract class, used with extends | interface, used with implements |
| Can have constructors and fields with state | Yes | No (only constants) |
| How many can a class use? | One | Many |
| Best for | Closely related classes sharing code | A 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
- Mixing up overloading (same name, different parameters) and overriding (same signature, in a subclass).
- Forgetting to call
super(...)when the parent has no default constructor. - Trying to create an object of an abstract class or interface.
- Making fields public "to make it work", which breaks encapsulation and loses marks in design questions.
- Using inheritance for a "has-a" relationship. A
Carhas anEngine; that's composition, not inheritance.