Table of contents
1.
Introduction
2.
What is OOPs concepts in Java?
3.
Inheritance
3.1.
Types of Inheritance
3.2.
Advantages of Inheritance
3.3.
Disadvantages of Inheritance
4.
Polymorphism
4.1.
Compile-time Polymorphism
4.2.
Runtime polymorphism 
5.
Abstraction  
5.1.
Advantages of abstraction 
6.
Encapsulation 
6.1.
Advantages of encapsulation 
7.
Frequently asked questions 
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Four Pillars of OOPS in Java

Author Rajkeshav
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

 

OOPS stands for object-oriented programming. All modern programming languages are based on the OOPS concept. Older programming languages like PASCAL, FORTRAN, C, etc., are not obsessed with objects. They are somewhat procedural programming languages.

What is OOPs concepts in Java?

Objects Oriented Programming is a programming style based on the idea of objects and classes to represent the Data. JAVA is based on object-oriented programming.

Class is not a real-world entity but a template or prototype. In Computer memory, Class doesn't occupy memory, but it imparts an idea about the features of objects. Objects, on the other hand, are the instances of the Class. An object is an actual entity that occupies space in the memory.  

Access Modifier:
The access modifier of a method defines its visibility to other classes and determines the level of access control. Common access modifiers include public, private, protected, and package-private (no explicit modifier). Public means the method can be accessed by any other class. Private restricts access to the class it belongs to. Protected allows access within its own package and by subclasses. Package-private (default, no modifier) restricts access to classes within the same package.

The Return Type:
This specifies the data type of the value that the method will return after its execution. If the method does not return any value, the return type is specified as void. Otherwise, it could be any data type like int, String, boolean, or any object type.

Method Name:
The method name is the identifier that is used to refer to it. It should follow the naming conventions of the language and typically begins with a lowercase letter. The name is usually a verb or verb phrase that clearly indicates the operation the method performs, such as calculateTotal, printDetails, or isEmpty.

Parameter List:
This is a sequence of variables enclosed in parentheses that are passed to the method. These are the inputs that the method uses to perform its operation. Each parameter is specified with a data type followed by an identifier name, and multiple parameters are separated by commas.

Exception List:
Some languages, like Java, allow methods to specify a list of exceptions that they might throw during the execution. This exception list alerts the callers of the method to provide necessary exception handling code. It's part of the method signature and is declared with the throws keyword followed by the exception types.

Method Body:
Enclosed in braces {}, the method body contains the code that defines what the method does. It consists of a series of statements that are executed when the method is called. This is where the actual logic of the method is implemented, and it's where variables are manipulated, other methods are called, loops are executed, and the return value is produced if applicable.

 

OOPS moves around its four most important pillars. These are; Inheritance, Polymorphism, Abstraction, and Encapsulation.

Let's discuss all these in brief.

Recommended topic Iteration Statements in Java.

Inheritance

Inheritance is a procedure to inherit the features from parent to child in the real world. Similarly, Inheritance in OOPS is a procedure by which one ClassClass acquires all the properties and behaviors of the parent class. Inheritance ensures the reusability of the code. For example, let there be a class named Animal having the feature called 'Eat' and 'Walk .' Let there be another class named 'Dog .'The 'Dog' class inherits all the features from the Animal class. So do we need to provide an Eat and Walk feature to the Dog class? No, we don't. These features are implicit in Dog class. 

Java uses the 'extends' keyword to acquire the features of the parent class. There is an 'IS-A' relationship between Dog and Animal class. We can write- Dog IS-A Animal. So Inheritance forms an IS-A relationship between the child and parent class. 

Click on the following link to read further Hashcode Method in Java

Types of Inheritance

 

There are a total of five types of Inheritance in OOPS.

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance
  • Hybrid Inheritance

 

JAVA does not provide Multiple and Hybrid Inheritance. We will go through each Inheritance in detail in upcoming blogs.

You can read more about Hybrid Inheritance in Java here.

Advantages of Inheritance

  • Code reusability
  • We can achieve Polymorphism using Inheritance. 

 

Disadvantages of Inheritance

The child class and the parent class are tightly coupled. Any changes in the parent class equally affect all the child classes.

Know about Single Inheritance in Java in detail.

Polymorphism

 

Poly means many, and morphism means forms. We know that Water also exists in multiple states, such as Solid, Liquid, and Gas. So Water shows Polymorphism. In Java, we can achieve Polymorphism using methods. There are two types of Polymorphism  that JAVA supports.

Also see,  Swap Function in Java

 

Compile-time Polymorphism

Compile-time Polymorphism is also known as static Polymorphism. We can achieve it by method overloading.

Method overloading satisfies three conditions.

  1. There must be at least two methods of the same name 
  2. Both the methods should be inside the same Class 
  3. Both methods must have different arguments

    =>The number of arguments is different 

                =>The sequence of arguments is different

                =>Type of argument is different 

 

The compiler handles Compile-time Polymorphism. 

 

Runtime polymorphism 

Runtime polymorphism is also called dynamic Polymorphism. We can achieve Runtime polymorphism by method overriding.

We can achieve method overriding by satisfying three conditions.

  1. There must be at least two methods of the same name 
  2. Both methods belong to different classes 
  3. Both methods must have the same arguments 

                =>The number of arguments is the same

                =>The sequence of arguments is the same

                =>Type of argument is the same

 

JVM handles the runtime polymorphism. 

Must Read Type Conversion in Java

Abstraction  

 

Abstraction is a property of hiding the internal implementation and highlighting the setup services beneficial to the user. For example, the smartphone user does not know the internal performance of the Smartphone and its workings; instead, they are interested in the services provided by the Smartphone. In Java, we can achieve abstraction in two ways.

  • Using abstract ClassClass
  • using interfaces 

 

Using abstract Class, we can achieve abstraction up to 0-100% but using the interfaces, we can achieve it up to 100%. 

 

Advantages of abstraction 

  • Reduces complexity
  • Avoid code duplication 
  • Ease the burden of maintenance
  • Increase security and confidentiality 

 

Encapsulation 

 

Encapsulation is a mechanism to bundle the data and code acting on the data together as a single unit. there are two steps to achieve encapsulation in Java

  • declare the variable of a class as private
  • Provide a Public setter and getter method to modify and view the values of the variables.

 

Advantages of encapsulation 

  • Loosely coupled code 
  • Better access control and security 
     

Must Read Conditional Statements in Java

Frequently asked questions 

 

1) What are access modifiers in Java? 

Access modifiers have a significant role in the security of the Java code. It can restrict the visibility of specific codes to certain types of users and give access to certain types of users. 

 

2) What are OOPs concepts in Java?

Object-oriented programming (OOP) in Java is built around four main concepts: Encapsulation, which bundles data with methods; Inheritance for hierarchical classification; Polymorphism, enabling objects to be treated as instances of their parent class; and Abstraction, which hides complex reality by simplifying reality.

 

3) What are the 4 pillars of Java? 

The four pillars of Java are Encapsulation, securing data within classes; Inheritance, for creating new classes from existing ones; Polymorphism, allowing methods to perform differently based on input; and Abstraction, hiding complex implementation details from the user.

Conclusion


The four pillars of Java—Encapsulation, Inheritance, Polymorphism, and Abstraction—constitute the foundation of its Object-Oriented Programming paradigm. Together, they provide a robust framework for creating modular, reusable, and maintainable code. They enable developers to model real-world scenarios effectively, leading to software that is both scalable and adaptable to evolving requirements.

The blog taught some basic building elements of OOPS. Must visit here for more detailed knowledge.

Recommended Readings: 

 

To learn more about Data Structures and Algorithms, you can enroll in our course on DSA in Java.

For a Complete and in-depth tutorial on OOPS in Java, you should watch the below video

Live masterclass