OOP Principles
Classes, objects and the four pillars: encapsulation, inheritance, polymorphism and abstraction.
#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:
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
class Employee {
int id;
String name;
}Creating objects:
Employee e1 = new Employee();
Employee e2 = new Employee();Memory — each object has its own state:
#What is an Object?
Object = Instance of a Class.
Car c = new Car();Here Car is the class and c is the object.
#Four Pillars of OOP
There are only four:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
#1. Encapsulation
Definition
Binding data + methods into one unit.
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:
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.
class Animal {
void eat() {}
}class Dog extends Animal {
}Dog automatically gets eat().
Memory:
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.
- Animal
- Dog
- Cat
- C
- A
- B
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
add(int)
add(double)
add(String)Same method, different parameters. The compiler decides.
Run Time — Method Overriding
Animal a = new Dog();
a.sound();Dog BarkingThe decision happens during runtime.
Example
class Animal {
void sound() {
System.out.println("Animal");
}
}class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}Animal a = new Dog();
a.sound();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
abstract class Animal {
abstract void sound();
}class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}Using Interface
interface Vehicle {
void start();
}class Car implements Vehicle {
public void start() {
}
}Abstract Class vs Interface
| Abstract class | Interface |
|---|---|
| Can have concrete methods | Mostly a contract |
| Can have constructors | Supports multiple inheritance |
| Can have variables | Default 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.