Oops in java

OOPs in Java & 4 Pillars of OOPs, Advantages & Why It Matters

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 givenOops in java

FeatureOOP (Java)Procedural
StructureBased on objectsBased on functions
Data HandlingEncapsulationGlobal data
ReusabilityHighLow
SecurityBetterWeak
ExampleJava, 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, likebenefits of OOPs

  1. Modularity
  2. Code reusability
  3. Improved security
  4. Flexible
  5. Scalable
  6. Maintainable

Pillars of Object-Oriented Programming:-

The main four pillars of object-oriented programming are as follows:

  1. Encapsulation
  2. Abstraction
  3. Inheritance
  4. 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 ModifierWithin ClassWithin PackageOutside 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

AdvantagesDisadvantages
Encapsulation protects data from unauthorized accessIt Can increase code complexity
Improves code maintainabilityMore code needed (getters/setters)
Makes the class easy to change internallySlower performance due to method calls
Enhances data securityToo 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.

FeatureAbstract ClassInterface
Keywordabstractinterface
MethodsIt Can have abstract and non-abstract methodsAll methods are abstract by default
VariablesIt Can have normal variablesVariables are public, static, and final
Multiple InheritanceNot supported (cannot extend multiple classes)A class can implement multiple interfaces.
ConstructorIt Can have constructorsIt Cannot have constructors
Access ModifiersMethods can have any access modifierMethods are public by default
UsageWhen you want partial abstractionWhen you want full abstraction
InheritanceClass extends abstract classA class implements an interface.

Advantage and Disadvantage Of Abstraction

AdvantagesDisadvantages
Hides unnecessary detailsCan be difficult to design correctly
Reduces complexityIncreases overall development time
Improves code readabilityRequires good understanding of OOP
Makes code easier to maintainToo 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: –Types of Inheritance

Type of InheritanceDescriptionExample
Single InheritanceIn single inheritance, one class extend another classCar extends Vehicle
Multilevel InheritanceIn multilevel inheritance, a chain of inheritance like grandfather to father to child classSedan extends Car, Car extends Vehicle
Hierarchical InheritanceIn 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 InheritanceThe mixing of one or more types of inheritance is called hybrid inheritance.Sedan extends Car and implements GPS interface

Advantage and Disadvantage Of Inheritance

AdvantagesDisadvantages
Promotes code reusabilityIncreases class dependency
Improves code structureChanges in parent class can affect child classes
Reduces redundancyCan lead to tight coupling
Supports polymorphismNot 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

  1. Compile-Time Polymorphism
  2. Runtime Polymorphism
FeatureCompile-Time PolymorphismRuntime Polymorphism
Also Known AsStatic Binding / Early BindingDynamic Binding / Late Binding
How It Is AchievedMethod OverloadingMethod Overriding
Decision TimeResolved at compile timeResolved at runtime
PerformanceFaster (less overhead)Slightly slower due to runtime decision
FlexibilityLess flexibleMore flexible (supports dynamic behavior)
Inheritance RequiredNot requiredRequired (works with inheritance)
Arguments / ParametersMust differ (type, number, order)Must be same as parent class method
Return TypeCan be different (in overloading)Must match or be covariant
ExampleMultiple methods with same name but different parametersChild class overriding the parent class method

Advantage and Disadvantage Of Polymorphism

AdvantagesDisadvantages
Increases code reusabilityCan make debugging more difficult
Makes code flexible and easier to extendOveruse may lead to complex class structures
Improves maintainabilityRuntime polymorphism can be slower
Reduces code duplicationRequires 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

What is Java

Tech Skills That Can Increase Your Salary in 2026

How to Get Your First Tech Job

AI Tools for Developers 2026

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.

Share your thoughts or Have a question? Comment below!

Scroll to Top