students and freshers learning programming

OOP Concepts — the four pillars, explained with code

Object-oriented programming (OOP) organises code around 'objects' that bundle data and behaviour together. Its four core pillars each solve a different problem in how code is structured and reused.

Encapsulation — bundling and protecting data

Encapsulation keeps an object's internal data private and exposes only controlled methods to interact with it. A BankAccount class might keep 'balance' private and only allow changes through a deposit()/withdraw() method, preventing invalid direct edits like balance = -500.

Inheritance — reusing behaviour

Inheritance lets a new class (subclass) reuse and extend an existing class's (superclass) code. A SavingsAccount class can inherit from BankAccount and reuse deposit()/withdraw(), while adding its own interest-calculation method.

Polymorphism — one interface, different behaviour

Polymorphism lets different classes respond to the same method call in their own way. Calling makeSound() on a Dog object and a Cat object runs different code in each, even though the calling code looks identical.

Abstraction — hiding unnecessary detail

Abstraction exposes only what's necessary to use an object, hiding internal implementation complexity. When you call car.start(), you don't need to know the internal ignition sequence — that complexity is hidden behind a simple method.

Examples

  • class Animal { makeSound() { return 'some sound'; } } class Dog extends Animal { makeSound() { return 'Woof'; } } — this is inheritance + polymorphism together.
  • A private balance field with public deposit()/withdraw() methods is encapsulation in action.
  • An abstract Shape class with an area() method that every subclass (Circle, Square) must implement differently is abstraction + polymorphism.

Common mistakes

  • Confusing inheritance (reusing a class's structure) with polymorphism (one method behaving differently per class).
  • Making all fields public 'for convenience', which defeats the purpose of encapsulation.
  • Overusing inheritance for code reuse when composition (an object containing another) would be simpler and more flexible.

Try it yourself

Which OOP pillar is demonstrated when a Circle class and a Square class both implement their own version of a calculateArea() method?

Show answer

Polymorphism — the same method name behaves differently depending on the actual object's class.

Frequently asked questions

Can Learn2Plus AI explain OOP in my preferred language?

Yes — tell Learn2Plus AI your language (Java, Python, C++, etc.) and every code example will use that language's syntax.

Is this asked in placement interviews?

Yes — OOP concepts are among the most commonly asked technical-fundamentals questions in placement interviews across almost every software role.

Can I get a small coding practice task on this?

Yes — ask for a small OOP practice task and you'll get one to attempt, with feedback after you try it.

Related topics

Practice OOP with Learn2Plus AI

Try 2 lessons free — no card required.