Nearby lessons

29 of 125

Java - OOPs Concepts

📌 What You Will Learn
  • 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:

GenerationStyleIdea in simple words
UnstructuredTop to bottom flowProgram runs line by line. Difficult when the program grows big.
StructuredFunctions (procedures)Divide the program into small functions. But data and functions are separate.
Object Oriented (OOP)Classes and objectsCombine data and the functions that use that data into one unit — the object. Best for big, real-world software.
Aspect Oriented (AOP)Cross-cutting concernsA 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.

Example02
JCode Cell
1class Account {
2 private double balance; // data hidden (private)
3 
4 public void setBalance(double b) { // public gate to set
5 if (b >= 0)
6 balance = b;
7 }
8 public double getBalance() { // public gate to read
9 return balance;
10 }
11}

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).
Example03
JCode Cell
1class Animal { // parent class (super class)
2 void eat() {
3 System.out.println("Animal is eating");
4 }
5}
6 
7class Dog extends Animal { // child class (sub class)
8 void bark() {
9 System.out.println("Dog is barking");
10 }
11}
12 
13class Test {
14 public static void main(String[] args) {
15 Dog d = new Dog();
16 d.eat(); // inherited from Animal
17 d.bark(); // own method
18 }
19}
Output
Animal is eating Dog is barking

The Four Pillars of OOP

Example04
JCode Cell
1class Calculator {
2 // overloading: same name, different parameters
3 int add(int a, int b) { return a + b; }
4 int add(int a, int b, int c) { return a + b + c; }
5 double add(double a, double b) { return a + b; }
6}
7 
8class Animal { void sound() { System.out.println("Some sound"); } }
9class Cat extends Animal {
10 // overriding: same signature, new behaviour
11 void sound() { System.out.println("Meow"); }
12}

Programming Paradigms — The Four Families

A programming paradigm is the style of writing a program. The classic material teaches four families and their differences:

ParadigmIdeaExamples
UnstructuredProgram runs top to bottom with goto jumps. No functions. Very old.BASIC, FORTRAN
StructuredProgram is divided into functions, with if/for/while for flow.C, Pascal
Object OrientedData 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

PointStructured (C)Object Oriented (Java)
Approach to build appsDifficultSimplified
ModularityNoYes (classes are modules)
AbstractionWeakStrong
Security of dataWeakStrong (encapsulation)
Code reusabilityLimited (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

PointObject Oriented (Java)Object Based (JavaScript)
Allows all 7 OO features?YesNo
InheritanceSupportedNot supported (in the classic sense)
ExampleJavaJavaScript

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.)

Example07
JCode Cell
11. Class 2. Object 3. Encapsulation 4. Abstraction
25. Inheritance 6. Polymorphism 7. Message Passing

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.
📝 Key Takeaways
  • 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).

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1