Object-oriented programming explained for students
8 min readUpdated
Object-oriented programming (OOP) organises a program around objects that hold data and the actions on that data. It is taught in school and university courses and asked about in nearly every programming interview.
Classes and objects
A class is a blueprint. An object is one thing built from it. A Car class says every car has a colour and can drive; myCar and yourCar are two objects with their own colours.
public class Car {
private String colour;
public Car(String colour) {
this.colour = colour;
}
public void drive() {
System.out.println("The " + colour + " car drives");
}
}
// elsewhere
Car myCar = new Car("red");
myCar.drive(); // The red car drivesPillar 1: encapsulation
Keep fields private and expose safe methods. The class controls its own data, so it can reject bad values.
public class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount <= 0) throw new IllegalArgumentException("Amount must be positive");
balance += amount;
}
public double getBalance() {
return balance;
}
}Pillar 2: inheritance
A subclass reuses and extends a parent class with extends. A Dog is an Animal, so it gets the animal's fields and methods.
Pillar 3: polymorphism
The same method call behaves differently depending on the actual object. A subclass overrides a method, and Java picks the right version at run time.
class Animal {
void speak() { System.out.println("..."); }
}
class Dog extends Animal {
@Override
void speak() { System.out.println("Woof"); }
}
// elsewhere
Animal a = new Dog();
a.speak(); // WoofPillar 4: abstraction
Show what an object does and hide how it does it. Abstract classes and interfaces describe the actions; concrete classes fill in the details.
interface Shape {
double area();
}
class Circle implements Shape {
private final double r;
Circle(double r) { this.r = r; }
public double area() { return Math.PI * r * r; }
}| Pillar | One-line meaning |
|---|---|
| Encapsulation | Hide data, expose safe methods |
| Inheritance | Reuse a parent class (is-a) |
| Polymorphism | One call, many behaviours |
| Abstraction | Show what, hide how |
Common misunderstandings
- A class is not an object;
newcreates the object. - Inheritance is not the only way to reuse code; composition (has-a) is often better.
- Overloading (same name, different parameters) is not the same as overriding.
- Making every field public breaks encapsulation even if you add getters.
Frequently asked questions
What are the four pillars of OOP?
Encapsulation, inheritance, polymorphism and abstraction.
Is Python object-oriented too?
Yes. The same ideas apply, with different syntax such as self and __init__.
What is the difference between a class and an object?
A class is the design; an object is an actual instance created from it with its own data.
Learn it step by step
Practise this, step by step
Short lessons with quizzes, practice and interview questions, from your first program to job-ready.