Understanding OOPs Concepts in Java

Understanding OOPs Concepts in Java

9 mins read19.1K Views Comment
clickHere
Updated on Apr 12, 2023 12:56 IST

Object oriented programming (OOP) concepts are the fundamental pillars of programming. The article below explains the OOPs concept in Java. It covers introductions to OOPs, class, objects, inheritance, abstraction, encapsulation, and polymorphism using relatable analogies. Moreover, it goes through the advantages and disadvantages of the OOPs concept in Java.

2022_03_Untitled-3.jpg

Originated in 1970, Object-oriented programming (OOPs) is one of the fundamental concepts used by almost all developers at some point in their careers. Just like C++, C#, and Python, Java is another programming language that uses object-oriented programming. Let’s drill down further and understand what is OOPs.

To learn more, Go for Best Courses on Java Programming.

What is OOPs Concept?

OOPs, or Object-Oriented Programming, is a fundamental programming concept which developers use for binding the data and function together using the concept of classes and objects. OOP concepts in Java help the programmer to control and access the data and improves code readability and reusability using the core concept of OOPs i.e., Abstraction, Encapsulation, Inheritance, and Polymorphism

Every class has some objects. Each object has some properties and methods associated with it. Together they build an object-oriented programing structure. For example, a class human can have an object as a name. The name object can store the phone number and email id of the person and methods used to send mail or call a number.

2022_09_image-102.jpg

Also Read: Variables in Java

Explore Best Online Java Courses

What are Classes and Objects in Java?

A Class is a logical entity that does not take any memory space without the creation of the object. In simple terms, a class is just like a blueprint of the Java program. In Java, we use the class keyword to declare a class. Objects, methods, variables, etc., are defined inside the class. For reference, the architectural diagram of a building (blueprint) can be considered a class, and the building itself is an object created as per the blueprint.

example1

Therefore, a class defines the working and structure of an object using data attributes (or variables) and member functions (or methods). Data attributes are the variables that help define the member functions (or methods).

NOTE: Every java program has at least one class which consists of the main method.

Syntax of Declaring a class in Java:

 
<access-modifier> class <className>
{
// data attributes;
// member functions()
}
Copy code

Also read: Access Modifiers in Java

A structure of a class consists of:

  • class keyword used to define a class.
  • Access Modifiers: A class can be public, package-level (default), private, or protected.
  • Class Name: Give the name of the class for accessing it.
  • Superclass: Mention the name of the superclass (if using any)
  • Interface: Mention the name of the super interface (if using any)
  • Body: Body of the class inside curly braces {}.

The object is an instance of a class. Object invokes a class. It means, by creating an object of a class, the logical entity gets recognition and space. A class can have several objects.

Let’s take an example to understand the above. We have a public class name, ‘Rectangle’ and a main class name ‘Demo.’ In the Demo class, we’ve created the object ‘myrec’ to invoke the class Rectangle.

code1

I hope this clears out the concept of classes and objects in OOPs. Now let’s understand how to use the concepts of objects and classes within the fundamental pillars of OOPs.

Read the Difference between JDK, JVM, and JRE.

Explore Free Online Courses to learn Java

Fundamental Pillars of OOPs Concept

There are 4 pillars of the OOPs concept. Let’s go through each one of them.

pillars

More blogs to explore on OOPs Concept:

All About Data Abstraction in Java
All About Data Abstraction in Java
Data abstraction in Java is the concept of hiding the internal details while only showing the functionality. This principle of object-oriented programming is implemented using abstract classes and methods.
Inheritance in Java
Inheritance in Java
Delve into the world of Java inheritance, discovering the "IS A" relationship between classes, superclass and subclass dynamics, and the nuances of method overriding. Understand how Java leverages inheritance for...read more
Polymorphism in Java: Meaning and its Types
Polymorphism in Java: Meaning and its Types
Polymorphism is a fancy-sounding word that really just means “many shapes.” And in the world of Java programming, it refers to the ability of objects to take on different forms...read more

Abstraction in Java

Data abstraction or hiding is an OOPs feature used to display only important information while hiding the implementation details from the user. Java uses the concepts of abstract classes and interfaces to achieve data abstraction.

For instance, While using a washing machine, all you do is press buttons; you don’t have to deal with the device’s internal wiring. Here the internal wiring details are abstracted for normal users.

example2

Another example is, Considering a database containing student information. When you want specific data, you run a query. Let’s say you want to fetch the names of the student whose avg. score is greater than 80. The query fetches the required data from the database while hiding the rest.  

For more information, read: Abstraction in Java

Encapsulation in Java

In simple words, encapsulation is the binding or wrapping up of data into a single unit. It is used to secure the data and hide the implementation details from users. It allows accessing a level without disclosing its complex details by declaring the objects private. 

For example, The easiest example to understand encapsulation is a bag. All the items within your bag are hidden from the outside.

For instance, Herbivorous animals are a class. All the animals that eat plants to survive come under this category. Hence, all the animals, such as cows, goats, deer, etc., encapsulate under a category of herbivorous animals. Moreover, the category distribution protects against other animal categories, such as carnivorous animals.

example3

Let’s see how it’s implemented in Java. The class can be a good example of Data encapsulation. Look how a class binds all the variables and methods together. In addition, using a private modifier protects the class or variable from outer access.

code2

Let’s take the example of a banking service app and understand the problem that encapsulation can solve. Within the app, any class which can access the customer object has to ability to access or modify the customer’s password. For example, in the BankingService class, the login method can directly access the saved password to check if it is valid; what if someone gets access to that method? They could easily change the password and retrieve all the amount :D. To avoid this scenario, the developers use encapsulation to ensure the contents of the password never get out of the Customer class. Encryption is further added as an extra layer to keep the password secure.

Read: Loops in Java Explained

Inheritance in Java

Inheritance is one of the most significant pillars of OOPs. It is a process in which one (child) class inherits (or uses) the property of another (superclass) class. It motivates the code reusability property in a program. As a new class derives the properties (or features) of an already existing class.

Let’s see a few terms used in the Inheritance concept:

  • Parent (or Super) class: A class whose properties get inherited by other sub-classes.
  • Child (or Sub) class: A new class inheriting an existing superclass’s properties.

Inheritance places a parent-child relationship (or hierarchical) among classes that influences code reusability in a program.

For instance, seeded fruits inherit the properties of Fruits. That means seeded fruit is a sub-class that shares the properties of super or base-class Fruits. Similarly, Apple can be a sub-class for the seeded fruits class. Here, seeded fruits can be parent class for apple class.

For more information, Read: Inheritance in Java

example4

Basic 3 types of inheritance in Java:

There are 4 types of inheritance in Java:

  • Single Inheritance: One-level inheritance where one subclass inherits the properties of a superclass.
  • Hierarchical Inheritance: Where one parent class has multiple child classes.
  • Multilevel Inheritance: Where class B inherits the properties of class A and class C inherits the properties of class B.
types

Note: Java does not support multiple and hybrid inheritances. Instead, it uses interfaces to achieve those functionalities.

Let’s see how inheritance gets implemented in Java using an example.

code5

We have a superclass, “Figure” and a child class “Rectangle.” The child class using the super keyword can access the variables of a parent class. In this case, the variable length and breadth of the Figure class are used by the Rectangle class.

Also Read: Final Keyword in Java

Polymorphism in Java

Polymorphism, as the name suggests, it means many forms. In simple terms, OOPs demonstrates how a single entity can have multiple forms. For instance, a woman can be seen as a lawyer, a mother, a sister, and a daughter. 

example6

Polymorphism is in 2 types: Compile-time polymorphism and Run-time Polymorphism. Compile-time polymorphism (or early binding polymorphism, resolved at compile time) expresses using overloading. And Run-time polymorphism (or late binding polymorphism, determined at run time) depicts using overriding. Commonly, the concept of polymorphism uses the same method name showing different operations.

For more information, Read: Polymorphism in Java

Let’s see how polymorphism in java is used. Here, we’re using the concept of method overload as an example.

code6

We can 3 methods (or functions) with the same name, ‘numbers’ with a different set of arguments. The object ‘o’ of class ‘Method-overload’ is aiming at different methods using the same name but different arguments. For instance, o.number(5) aims for the second method, which uses ‘int a’ in the argument.

Read: Implementing Array in Java

The Power of Enum Types in Java: Examples and Use Case
The Power of Enum Types in Java: Examples and Use Case
In this article, we will discuss what is enum in java, how to implement it, important methods and three special cases of enum.
Java Lambda Expression
Java Lambda Expression
Java Lambda Expression is a way to define an anonymous function or method that can be passed around as a value. It allows you to write code that is more...read more
Understanding Java Scanner Class
Understanding Java Scanner Class
In this article, you will learn about Java Scanner class. We have also covered Java user input, input types, etc., relevant topics. Let’s begin!

Advantages of OOPs Concept

The following are the benefit of the OOPs concept:

  • Using the OOPs methodology, one can enhance the code reusability and save development time.
  • Easy message passing establishes communication between classes and objects.
  • Using functionalities like data abstraction and hiding, OOPs ensure the security of the code.
  • OOP concepts like inheritance help to eliminate the need for code redundancy.
  • It expresses modular structure and flexibility, which results in easy maintenance.

Read: Access Modifiers in Java

Disadvantages of OOPs Concept

The following are the limitations of the OOPs concept:

  • The size of a program using the OOP paradigm is much larger than other methodologies.
  • Requires much effort to create a perfect OOP concept-based code.
  • OOP concept-based programs are slower to execute than others.
  • OOP concept can’t be applied to every solution.

For more, Read: 8 Most Important Data Structures a Programmer Must Know

Also Read: Data Types in Java – Primitive and Non-Primitive Data Types Explained

Conclusion

In the above article, we went through the principles of OOPs using the Java programming language. First, we understood the concept of classes and objects. Further, we covered all 4 pillars of OOPs, abstraction, encapsulation, inheritance, and polymorphism, using simple analogies. We also did a few hands to understand the above concepts.

I hope you learned and enjoyed reading the article. Please share your suggestion below. We’ll come up with more complex subjects explained using simple analogies. 

For more insights, you can visit the OOP concept in Python.

FAQs

What is the main difference between procedural and Object-oriented programming?

Procedural programming uses module methodology (or functions). OOP uses the concept of objects and classes.

What is Data hiding in Abstraction?

Data abstraction or data hiding is an OOPs feature which is use to display only important information while hiding the implementation details from the user.

What is Compile-time Polymorphism?

Compile-time polymorphism (also called early binding or static polymorphism) resolves the program at compile-time. It expresses using method overloading.

What is Run-time Polymorphism?

Run-time polymorphism (also called late binding or dynamic polymorphism) resolves the program at run-time. It expresses using method overriding.

What is the full form of OOP?

OOPS full form is Object Oriented Programming.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio

Comments