Understanding OOPS Concepts in Java
Object Oriented Programming (OOPs) is one of the most important topics in Java and is frequently asked in interviews. In this blog, I will explain OOPS concepts in Java in simple language with examples.
1. What is OOPS?
In OOPS, the program is divided into objects, not functions. First let us know about Procedural Programming.
In Procedural programming, program is divided into functions.
There is no data hiding, inheritance, code reusability and overloading also not possible.
In OOPS,
Program is divided into objects
supports data hiding
supports inheritance
overloading is possible
helps in code reusability
Examples :
Procedural languages: C, Pascal, Fortran
OOPS languages: Java, C++, Python, C#
2. Object and Class
What is Object?
Object is a real world entity that has:
Properties(State)
Behavior(Functions)
Example:
Dog Object:
Properties: age, color, breed
Behavior: bark(), speed()
What is Class?
Class is a blueprint of an Object.
We create objects using class.
From one class we can create multiple objects.
Example:
class Student{
int age;
String name;
String address;
void updateAddress(){
System.out.println("Address Updated");
}
int getAge(){
return age;
}
}
Create Object:
Student s1 = new Student();
3. Four Pillars of OOPS
OOPS has 4 main pillars:
Abstraction
Encapsulation
Inheritance
Polymorphism
Now let us learn more about these 4 pillars:
1. Abstraction
Abstraction means hiding internal implementation and showing only required functionality.
Example:
In car -> we press brake, but we don't know internal mechanism
In mobile -> we call someone, but we don't know internal working
This is abstraction and acheived using:
interface
abstract class
Example:
interface Car{
void applyBrake();
void increaseSpeed();
}
Implementation:
class Bentley implements Car{
public void appyBrake(){
Sysem.out.println("Brake applied");
}
public void increaseSpeed(){
System.out.println("Speed increased");
}
}
Advantages:
Security
Data hiding
Easy to use
Less complexity
2.Encapsulation
Encapsulation means Binding data and methods into one unit. Also called as Data Hiding.
Steps to achieve encapsulation
Make variables private
Use getters and setters
Example:
class Dog{
private String color;
public String getColor(){
return color;
}
public void setColor(String color){
this.color=color;
}
}
Use Object:
Dog d = new Dog();
d.setColor("Black");
System.out.println(d.getColor());
Advantages:
Security
Better control
Loosely coupled code
3.Inheritance
Inheritance means, A class can acquire properties and methods of another class. It helps in code reusability. we use the keyword 'extends'.
Example:
Parent Class:
class Vehicle{
boolean engine = true;
boolean hasEngine(){
return engine;
}
}
Child Class:
Class car extends Vehicle{
String type = "petrol";
String getType(){
return type;
}
}
Create Object:
Car c = new Car();
System.out.println(c.hasEngine());
System.out.println(c.getType());
Child class can access parent properties. But parent cannot access child properties.
Types of Inheritance:
- Single Inheritance
A -> B
class A{}
class B extends A{}
- Multilevel Inheritance
A -> B -> C
class A{}
class B extends A{}
class C extends B{}
- Hierarchial Inheritance
class A{}
class B extends A{}
class C extends A{}
- Multiple Inheritance:
Not supported in Java using classes because of Diamond problem. But possible using Interface.
Advantages of Inheritance:
Code reusability
Less code
Easy maintenance
Supports polymorphism
7.Polymorphism
Polymorphism means, one method many forms. Same method behaves differently.
Example:
- Person -> father/ employee/ student
Types of Polymorphism:
Compile time polymorphism (Method Overloading)
Runtime polymorphism (Method Overriding)
Method Overloading:
Same method name, different parameters
Example:
class Sum{
int add(int a, int b){
return a+b;
}
int add(int a, int b, int c){
return a+b+c;
}
}
Create Object:
Sum s = new Sum();
System.out.println(s.add(2,3));
System.out.println(s.add(2,,3,4));
Compiler decides which method to call. So this is a Compile Time Polymorphism.
Method Overriding:
Same method name, same parameters, but different implementation is called Method Overriding.
Example:
class A{
int getValue(){
return 1;
}
}
class B extends A {
int getValue(){
return 2;
}
}
Create Object:
B obj = new B();
System.out.println(obj.getValue()); // 2
Method decided at runtime, so this is Runtime Polymorphism.
4. IS-A Relationship
IS - A means inheritance.
Example:
Dog IS-A Animal
Car IS-A Vehicle
5.HAS-A Relationship
HAS-A means one object uses another object.
Example:
School has Students
Bike has Engine
Car has Wheel
Association, Aggregation, Composition:
- Association:
Relationship between two objects.
Example:
Teacher <-> Students
- Aggregation:
Objects can exist independently.
Example:
School -> Students
If school removed, student still exists.
- Composition
Objects depend on each other.
Example:
House -> Room
If house is deleted, then room also deleted.
Conclusion
Object Oriented Programming(OOPS) is the foundation of Java and one of the most important concepts for every developer. Understanding OOPS helps us to write clean, reusable, secure and maintainable code. In real world software development, almost every application is built using OOPS concepts. Features like Encapsulation, Abstraction, Inheritance, and Polymorphism make programs more flexible and easy to manage.






