Nearby lessons
29 of 125Java - OOPs Concepts
- What class and object mean, with real-life examples
- The four pillars: Encapsulation, Abstraction, Inheritance, Polymorphism
- Constructors, this, super, static and final
- Method overloading and method overriding (with a comparison table)
- Abstract classes and interfaces (including the modern changes)
- Association, Aggregation and Composition
OOPs Concepts is a core concept of the Java language. This lesson explains How Programming Has Developed, The Four Pillars of OOP and Programming Paradigms — The Four Families with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
How Programming Has Developed
Programming languages developed step by step. Understanding this journey makes OOP easy to love:
| Generation | Style | Idea in simple words |
|---|---|---|
| Unstructured | Top to bottom flow | Program runs line by line. Difficult when the program grows big. |
| Structured | Functions (procedures) | Divide the program into small functions. But data and functions are separate. |
| Object Oriented (OOP) | Classes and objects | Combine data and the functions that use that data into one unit — the object. Best for big, real-world software. |
| Aspect Oriented (AOP) | Cross-cutting concerns | A special style that keeps common tasks (like logging) separate. This is an advanced topic. |
In structured programming, the data and the functions are separate. Example: you keep your money (data) in one place and the rules to spend it (functions) somewhere else — anyone can access anything, which creates problems. OOP fixes this by keeping the money and the rules together inside a box called an object.
The Four Pillars of OOP
Pillar 1 — Encapsulation
Encapsulation means wrapping data and the methods that work on that data together, and hiding the data from outside. It is like a capsule (medicine) — the medicine is inside, and you can only use it through the outer shell.
In Java we achieve encapsulation by making variables private and giving public getter and setter methods to read and update them.
Pillar 2 — Abstraction
Abstraction means showing only the necessary things and hiding the internal details. When you press the accelerator of a car, you do not need to know how the engine works — the car shows you a simple control. Abstraction in Java is achieved through abstract classes and interfaces.
Pillar 3 — Inheritance
Inheritance means a child class reuses the properties and methods of a parent class. Like a child inheriting features from parents, a class can inherit code from another class. This removes repetition.
The Four Pillars of OOP
Pillar 4 — Polymorphism
Polymorphism means one name, many forms. The same action behaves differently in different situations. In Java there are two types:
- Compile-time polymorphism — method overloading (same method name, different parameters).
- Run-time polymorphism — method overriding (child redefines parent method).
The Four Pillars of OOP
Programming Paradigms — The Four Families
A programming paradigm is the style of writing a program. The classic material teaches four families and their differences:
| Paradigm | Idea | Examples |
|---|---|---|
| Unstructured | Program runs top to bottom with goto jumps. No functions. Very old. | BASIC, FORTRAN |
| Structured | Program is divided into functions, with if/for/while for flow. | C, Pascal |
| Object Oriented | Data and functions live together in objects. Best for big apps. | Java, C++ |
| Aspect Oriented (AOP) | OOP + separating services (logging, security) from business logic. | Spring AOP |
Structured vs OOP — the differences
| Point | Structured (C) | Object Oriented (Java) |
|---|---|---|
| Approach to build apps | Difficult | Simplified |
| Modularity | No | Yes (classes are modules) |
| Abstraction | Weak | Strong |
| Security of data | Weak | Strong (encapsulation) |
| Code reusability | Limited (functions) | Very good (inheritance) |
What is Aspect Oriented Programming (AOP)?
In plain OOP, business logic and service logic (logging, security, transactions) are written together — this makes a tightly coupled design. AOP separates every service into an aspect and injects it into the application at run time. The result is a loosely coupled design with better reusability. (Spring's AOP is built on exactly this idea — you will meet @Aspect in Spring.)
Object Oriented vs Object Based
| Point | Object Oriented (Java) | Object Based (JavaScript) |
|---|---|---|
| Allows all 7 OO features? | Yes | No |
| Inheritance | Supported | Not supported (in the classic sense) |
| Example | Java | JavaScript |
The rule to remember: object-oriented = all features including inheritance; object-based = all features except inheritance.
The 7 OOP Features
The classic material lists exactly seven object-oriented features:
Message passing is the 7th feature: transferring data along with the flow of execution from one instruction to another. Its advantages are better communication and data navigation between the parts of a program. (In Java, this happens through method calls — you pass arguments and the method returns a result.)
OOP Interview Favourites — Quick Points
- Java does not support multiple inheritance with classes (to avoid the diamond problem). It supports it with interfaces.
- private, protected, public — the access modifiers control who can see what (details in the Packages chapter).
- A class can have only one public class per file, and that file is named after it.
- toString(), equals(), hashCode() are methods that every object inherits from the Object class — the root parent of all classes.
- Class = blueprint; Object = real thing made from it (memory in heap).
- Four pillars: Encapsulation (hide data), Abstraction (show only essentials), Inheritance (reuse parent code), Polymorphism (one name, many forms).
- Overloading = same class, different parameters, decided at compile time. Overriding = child redefines parent method, decided at run time.
- Constructor initialises the object; name = class name, no return type.
- this = current object; super = parent class; static = belongs to class; final = cannot change.
- abstract class cannot be instantiated; interface is a contract a class implements.
- Association (weak), Aggregation (has-a, part independent), Composition (strong has-a, part dies with whole).