The 4 Types of OOPS Concepts, Explained With Examples
The four types of OOPS concepts are encapsulation, inheritance, polymorphism and abstraction. Object oriented programming means organising code around objects, and these four ideas are how it does that.
| Concept | One line definition |
|---|---|
| Encapsulation | Keep data and its methods in one class, and hide the data |
| Inheritance | A new class reuses the fields and methods of an existing class |
| Polymorphism | The same call runs different code depending on the real object |
| Abstraction | Expose what an object does and hide how it does it |
Some lists count classes and objects as a fifth item. The four above are the set interviewers expect.
Encapsulation
Encapsulation means the data of an object is private, and the only way in is through methods you control.
The benefit is that you can check every change. A balance that can only move through deposit and withdraw can never go negative by accident.
class BankAccount { private double balance; // hidden data public void deposit(double amount) { if (amount > 0) balance += amount; // the rule lives here } public double getBalance() { return balance; } }
Nothing outside the class can write balance. That is the whole point.
Inheritance
Inheritance lets a new class take the fields and methods of an existing one. The existing class is the parent or base class. The new one is the child or derived class.
It is often asked in a second form, the types of inheritance.
| Type | Shape |
|---|---|
| Single | One child from one parent |
| Multilevel | A child of a child, three or more levels deep |
| Hierarchical | Several children from the same parent |
| Multiple | One child from two or more parents |
| Hybrid | A mix of the types above |
Java and C# do not allow multiple inheritance of classes. You get the same effect by implementing several interfaces. C++ does allow it, which is why it needs virtual inheritance to solve the diamond problem.
Prefer composition when the relationship is not a true "is a". A Car has an Engine, so it should hold one, not extend one.
Polymorphism
Polymorphism means one name and many behaviours. It comes in two kinds.
- Compile time polymorphism. Method overloading. Two methods share a name and differ in their parameters, and the compiler picks one.
- Run time polymorphism. Method overriding. A child class replaces a parent method, and the real object type decides which runs.
Abstraction
Abstraction means you publish the operations and hide the work behind them. Abstract classes and interfaces are the usual tools.
This example shows abstraction, inheritance and run time polymorphism together.
abstract class Animal { // abstraction abstract String speak(); } class Dog extends Animal { // inheritance String speak() { return "Woof"; } } class Cat extends Animal { String speak() { return "Meow"; } } public class Demo { public static void main(String[] args) { Animal a = new Dog(); System.out.println(a.speak()); // prints Woof } }
The variable is declared as Animal, so the caller does not know or care which animal it holds.
Classes you will be asked about
A follow up question often asks about kinds of classes, which is a different list.
- Concrete class. A normal class you can create objects from.
- Abstract class. Cannot be created directly, and may leave methods unimplemented.
- Interface. Only a set of method signatures, with no state.
- Final or sealed class. Cannot be extended.
- Nested and inner class. A class declared inside another class.
How to Prepare
- Learn one sentence and one example for each concept. That pair answers the question in under a minute.
- Expect the "difference between" follow ups. Abstraction versus encapsulation, and overloading versus overriding, are the two most common.
- Design a small system on paper. A parking lot, a library or an elevator shows all four concepts in one answer.
- Practice saying it out loud. A mock interview exposes definitions you know but cannot explain. See also what are the types of mock interview.
- Move from classes to services. The same boundaries scale up. Grokking the System Design Interview shows how.
- Keep the coding rounds in view. Theory questions are short, and the algorithm round is longer. Grokking the Coding Interview covers that side.

GET YOUR FREE
Coding Questions Catalog

$98

$197

$72