DevNotes
Support
Core Java · Chapter 01

OOP Principles

Classes, objects and the four pillars: encapsulation, inheritance, polymorphism and abstraction.

◷ 6 min read§ 11 sections? 16 interview questions{ } 16 code examples

#What is OOP?

OOP stands for Object Oriented Programming. Everything revolves around:

  • Objects
  • Classes
  • Relationships

Instead of writing functions separately, we bundle data and methods inside one object.

For example, instead of keeping deposit(), withdraw() and balance as separate pieces, we create an Account that contains everything:

deposit() · withdraw() · balanceseparate pieces
Accountbalancedeposit()withdraw()transfer()
An Account object owns its state and behaviour

This is called an Object.

#What is a Class?

A class is a blueprint. A house blueprint is not a real house, but using the blueprint we build House1, House2, House3.

Similarly, class Employee is only a blueprint. The objects are Employee e1, Employee e2, Employee e3.

  • Employee (class)
    • e1
    • e2
    • e3
One class, many objects
Java
class Employee {

    int id;
    String name;

}

Creating objects:

Java
Employee e1 = new Employee();
Employee e2 = new Employee();

Memory — each object has its own state:

e1
Employee objectid = 101name = John
e2
Employee objectid = 102name = David

#What is an Object?

Object = Instance of a Class.

Java
Car c = new Car();

Here Car is the class and c is the object.

#Four Pillars of OOP

There are only four:

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

#1. Encapsulation

Definition

Binding data + methods into one unit.

Java
class Employee {

    private int salary;

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public int getSalary() {
        return salary;
    }

}

salary is hidden. It can be accessed only through methods.

Why private?

Imagine anyone could write salary = -50000. Anyone can modify it — that is wrong.

Instead, setSalary() can validate:

Java
public void setSalary(int salary) {

    if (salary > 0)
        this.salary = salary;

}

Now the object is protected.

Real-life example: ATM

You cannot access balance directly. You can only Withdraw, Deposit and Check Balance. That is exactly encapsulation.

Advantages

  • Data Hiding
  • Security
  • Easy Maintenance
  • Loose Coupling

#2. Inheritance

Definition

Acquiring properties from another class.

Parent
Child
Java
class Animal {

    void eat() {}

}
Java
class Dog extends Animal {

}

Dog automatically gets eat().

Memory:

Animaleat()sleep()
Dogeat()sleep()bark()
Dog inherits eat() and sleep(), and adds bark()

Advantages

  • Code Reuse
  • Easy Maintenance
  • Less Duplicate Code

Types of Inheritance

Java supports Single, Multilevel and Hierarchical inheritance.

Java does not support Multiple Inheritance (with classes) because of the Diamond Problem.

A
B
C
Multilevel: C extends B, B extends A
  • Animal
    • Dog
    • Cat
Hierarchical
  • C
    • A
    • B
Multiple inheritance: not allowed with classes, allowed with interfaces
Java
class A {

    void show() {}

}

class B extends A {

    void show() {}

}

class C extends A {

    void show() {}

}

class D extends B, C {   // not allowed in Java

}

The compiler cannot decide which show() should execute. Hence Java avoids this.

#3. Polymorphism

Poly = Many, Morph = Forms. One object, many behaviours.

There are two types:

  • Compile Time
  • Run Time

Compile Time — Method Overloading

Text
add(int)
add(double)
add(String)

Same method, different parameters. The compiler decides.

Run Time — Method Overriding

Java
Animal a = new Dog();

a.sound();
Output
Dog Barking

The decision happens during runtime.

Example

Java
class Animal {

    void sound() {
        System.out.println("Animal");
    }

}
Java
class Dog extends Animal {

    void sound() {
        System.out.println("Bark");
    }

}
Java
Animal a = new Dog();

a.sound();
Output
Bark

#4. Abstraction

Showing only essential details. Hiding implementation.

Example — ATM: you click Withdraw. You don't know about the SQL, the server or the cash counting. All of that is hidden.

Using Abstract Class

Java
abstract class Animal {

    abstract void sound();

}
Java
class Dog extends Animal {

    void sound() {
        System.out.println("Bark");
    }

}

Using Interface

Java
interface Vehicle {

    void start();

}
Java
class Car implements Vehicle {

    public void start() {

    }

}

Abstract Class vs Interface

Abstract classInterface
Can have concrete methodsMostly a contract
Can have constructorsSupports multiple inheritance
Can have variablesDefault methods
Static methods
Private methods (Java 9)
  • Payable (interface)
    • Employee
    • Vendor
    • Customer

#OOP Interview Questions

Q1What are the four pillars?

Encapsulation, Inheritance, Polymorphism, Abstraction.

Q2Which principle provides security?

Encapsulation.

Q3Which principle enables code reuse?

Inheritance.

Q4Compile-time polymorphism?

Method Overloading.

Q5Runtime polymorphism?

Method Overriding.

Q6Can we instantiate an abstract class?

No.

Q7Can an interface have methods?

Yes.

  • Java 8+ → abstract, default and static methods
  • Java 9+ → private methods
Q8Which keyword creates inheritance?

extends

Q9Which keyword implements an interface?

implements

Q10Why use abstraction?

To expose only required functionality while hiding implementation details, improving maintainability and reducing complexity.

#Interview Tips (5 Years Experience)

Interviewers rarely stop at definitions. Be prepared to explain where you've used each concept in real projects:

  • Encapsulation: DTOs, entities with private fields and controlled access through getters/setters.
  • Inheritance: Base exception classes, common service implementations, or abstract controllers.
  • Polymorphism: Strategy pattern, payment processors, notification services, dependency injection with interfaces.
  • Abstraction: Service interfaces (UserService), repositories, external API clients, and framework contracts.

#Chapter Summary

Topics covered:

  • Class & Object
  • OOP Fundamentals
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction
  • Common interview questions
  • Real-world examples

Next: SOLID Principles — one of the most frequently asked topics in Java/Spring Boot interviews for 5+ years of experience.