Last Updated on April 11, 2026 by Rex
OOPs in Java: If we talk about Java, the most important concept we need to know is OOPs. OOPs helps you to write clean, Organized and scalable code. Why “oops” is better than procedural is given
| Feature | OOP (Java) | Procedural |
|---|---|---|
| Structure | Based on objects | Based on functions |
| Data Handling | Encapsulation | Global data |
| Reusability | High | Low |
| Security | Better | Weak |
| Example | Java, C++ | C, Pascal, |
What is OOPs in Java
OOPs stands for Object-Oriented Programming. It is a Java programming paradigm that focuses on creating objects that contain both data and behavior. Before starting the pillar of OOP, first we have to know about classes and objects.
Class
A class is a blueprint or, say, template of an object. It represents the set of properties or methods. Class is a logical entity. It is declared with the keyword class. And it does not take memory, but the object takes it when created.
Object
An object is an instance of a class. It is created with the class, holds the property values, and can use the methods. An object is a real entity, not logical like a class. It takes memory when it is created. In short, we say class is the template, and the object is the thing made from that template. A home is made from a map. Just like that, the home map is a class, and the home is a real entity,, like an object. It is created using a new keyword.
Benefits of Object-Oriented Programming:
There are many Benefits of OOPs, like
- Modularity
- Code reusability
- Improved security
- Flexible
- Scalable
- Maintainable
Pillars of Object-Oriented Programming:-
The main four pillars of object-oriented programming are as follows:
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
1-Encapsulation
The binding of the data and methods together inside the single unit, i.e., class, is called encapsulation. It helps in hiding the internal data and accessing it by using getter and setter methods. It is also known as data hiding. It is used for security reasons.
Access Modifier
The four access modifiers are as follows: –
| Access Modifier | Within Class | Within Package | Outside Package (Subclass) | Outside Package (Non-Subclass) | Description |
|---|---|---|---|---|---|
| public | ✔️ | ✔️ | ✔️ | ✔️ | Accessible from everywhere. |
| protected | ✔️ | ✔️ | ✔️ | ❌ | It can be accessed everywhere except outside the package. |
| default (no keyword) | ✔️ | ✔️ | ❌ | ❌ | It can be accessed only within the same package. |
| private | ✔️ | ❌ | ❌ | ❌ | It can be accessed in the same class. |
Advantage and Disadvantage Of Encapsulation
| Advantages | Disadvantages |
|---|---|
| Encapsulation protects data from unauthorized access | It Can increase code complexity |
| Improves code maintainability | More code needed (getters/setters) |
| Makes the class easy to change internally | Slower performance due to method calls |
| Enhances data security | Too much encapsulation can reduce flexibility |
2-Abstraction
To hide the internal implementation and show only the necessary things is called abstraction. For example: Switching on the fan no matter what—to know the mechanism of the fan and its working. It is one of the most important pillars of the oops.
In Java we can achieve abstraction with abstract classes and interfaces.
| Feature | Abstract Class | Interface |
|---|---|---|
| Keyword | abstract | interface |
| Methods | It Can have abstract and non-abstract methods | All methods are abstract by default |
| Variables | It Can have normal variables | Variables are public, static, and final |
| Multiple Inheritance | Not supported (cannot extend multiple classes) | A class can implement multiple interfaces. |
| Constructor | It Can have constructors | It Cannot have constructors |
| Access Modifiers | Methods can have any access modifier | Methods are public by default |
| Usage | When you want partial abstraction | When you want full abstraction |
| Inheritance | Class extends abstract class | A class implements an interface. |
Advantage and Disadvantage Of Abstraction
| Advantages | Disadvantages |
|---|---|
| Hides unnecessary details | Can be difficult to design correctly |
| Reduces complexity | Increases overall development time |
| Improves code readability | Requires good understanding of OOP |
| Makes code easier to maintain | Too much abstraction can confuse beginners |
Inheritance
Inheritance means to pass the properties and methods of the parent class to its child class. For example, in real life, children inherit properties from their parents, such as some of your traits matching your father’s.
This is the parent class
class Vehicle {
String color = “Black”;
int wheels = 4;void start() {
System.out.println(“Vehicle is starting…”);
}
}This is the Child Class which inherit the Vehicle class using extend keyword
class Car extends Vehicle {
String model = “Sedan”;void showDetails() {
System.out.println(“Color: ” + color);
System.out.println(“Wheels: ” + wheels);
System.out.println(“Model: ” + model);
}
}
Types of Inheritance
The types of inheritance and differences are given below: –
| Type of Inheritance | Description | Example |
|---|---|---|
| Single Inheritance | In single inheritance, one class extend another class | Car extends Vehicle |
| Multilevel Inheritance | In multilevel inheritance, a chain of inheritance like grandfather to father to child class | Sedan extends Car, Car extends Vehicle |
| Hierarchical Inheritance | In hierarchical inheritance, multiple classes inherit one parent class. | FinanceBlog & JavaBlog extend Blog |
| Multiple Inheritance | In multiple inheritance, one class implements multiple interfaces. Java does not support this. | class SmartDevice implements Camera, MusicPlayer |
| Hybrid Inheritance | The mixing of one or more types of inheritance is called hybrid inheritance. | Sedan extends Car and implements GPS interface |
Advantage and Disadvantage Of Inheritance
| Advantages | Disadvantages |
|---|---|
| Promotes code reusability | Increases class dependency |
| Improves code structure | Changes in parent class can affect child classes |
| Reduces redundancy | Can lead to tight coupling |
| Supports polymorphism | Not suitable for all scenarios (e.g., multiple inheritance in Java not allowed) |
PolyMorphism
Polymorphism is made up of two Greek words. Poly=many, morph=forms. That allows objects to behave differently based on their specific class type. This means it can take multiple forms.
Types of PolyMorphism
- Compile-Time Polymorphism
- Runtime Polymorphism
| Feature | Compile-Time Polymorphism | Runtime Polymorphism |
|---|---|---|
| Also Known As | Static Binding / Early Binding | Dynamic Binding / Late Binding |
| How It Is Achieved | Method Overloading | Method Overriding |
| Decision Time | Resolved at compile time | Resolved at runtime |
| Performance | Faster (less overhead) | Slightly slower due to runtime decision |
| Flexibility | Less flexible | More flexible (supports dynamic behavior) |
| Inheritance Required | Not required | Required (works with inheritance) |
| Arguments / Parameters | Must differ (type, number, order) | Must be same as parent class method |
| Return Type | Can be different (in overloading) | Must match or be covariant |
| Example | Multiple methods with same name but different parameters | Child class overriding the parent class method |
Advantage and Disadvantage Of Polymorphism
| Advantages | Disadvantages |
|---|---|
| Increases code reusability | Can make debugging more difficult |
| Makes code flexible and easier to extend | Overuse may lead to complex class structures |
| Improves maintainability | Runtime polymorphism can be slower |
| Reduces code duplication | Requires strong understanding of OOP concepts |
Common Interview Questions
1- What is OOP in Java?
OOP in Java stands for Object-Oriented Programming, which is a programming approach that uses classes and objects to structure code. It helps in making applications modular, reusable, and easy to maintain. Java follows four main OOP principles: encapsulation, abstraction, inheritance, and polymorphism. These concepts help developers build scalable and secure applications.
2- Tell me how abstraction and encapsulation are different.
Abstraction means hiding implementation details and showing only essential functionality to the user, while encapsulation means wrapping data and methods together and protecting data using access modifiers like private and public. In simple terms, abstraction focuses on what to show, and encapsulation focuses on how to protect data.
3- What is method overloading?
Method overloading is a feature in Java where multiple methods have the same name but different parameters in the same class. It helps improve code readability and allows methods to perform different operations based on input. It is also known as compile-time polymorphism because the method call is resolved during compilation.
4- What is method overriding?
Method overriding is when a child class provides its own implementation of a method that is already defined in the parent class. It is used to achieve runtime polymorphism and allows dynamic method execution based on the object. The method name and parameters must be the same as the parent class method.
5- What is inheritance?
Inheritance is a concept in Java where one class acquires the properties and methods of another class using the extends keyword. It helps in code reusability and reduces duplication. For example, a dog class can inherit common behavior like eat() from an animal class.
6- Can Java support multiple inheritance?
Java does not support multiple inheritance with classes to avoid the diamond problem, which creates ambiguity in method calls. However, Java supports multiple inheritance using interfaces, where a class can implement multiple interfaces and inherit their methods.
7- What is polymorphism?
Polymorphism means one method behaving in multiple ways depending on the object. In Java, it is achieved using method overloading and method overriding. Overloading provides compile-time polymorphism, and overriding provides runtime polymorphism, making the code flexible and dynamic.
8- Is Java a pure OOP? Why?
No, Java is not a pure object-oriented language because it supports primitive data types like int, float, and boolean, which are not objects. In pure OOP languages, everything is treated as an object, but Java uses both objects and primitive types, so it is considered partially object-oriented.
For official documentation, see Oracle Java Tutorials
Tech Skills That Can Increase Your Salary in 2026
How to Get Your First Tech Job
Best Tech Skills That Generate Passive Income in 2026
New Rule for IT Jobs in 2026 May Affect Fresher Hiring in India
Discover more from GroWithMoney
Subscribe to get the latest posts sent to your email.



