Introduction to OOPs Concepts in Java

OOPs refers to an Object-oriented programming system which is basically a paradigm or a methodology to design programs using concepts called classes and objects.

So everything in a pure Object-oriented language is built by using objects. Over the years, many languages have evolved which use this Object-oriented paradigm, starting from Simula to Ruby, while java, python, and C++ being the major Object-oriented languages. 

In this blog, we shall majorly discuss the Object-oriented nature of Java and learn more about the OOPs Concepts in Java

The major question is that “Is  JAVA a pure object-oriented programming language?’’

The answer is NO. Java is not considered as the purest form but it is almost 95% of Object-oriented nature. The reason is due to the presence of primitive data types in java. As it is already mentioned  that everything in an object-oriented language should be objects. But primitive data types in java are neither classes nor objects but they are some inbuilt keywords.

Object and Class

So, first we shall clearly learn about the concept of object and class.

OOPs methodology has been derived from a single root called Object. The object is a real-world entity that has some state and behavior. State includes the characteristics or properties of that object while the behavior includes the tasks performed by the objects. Programmatically, we call variables as state and methods as behavior.

Collection of all these similar kinds of Objects gives us the class. So, a class in Java is defined as the collection of objects with similar states and behavior. It is a logical entity which means that it can’t be touched, it can only be imagined logically.

A real-world example regarding classes and objects as OOPs Concepts in Java is discussed below.

We have seen the evolution of phones over the years. There used to be phones called rotary phones, then keypad phones, and now smartphones. All these are considered to be objects because they had similar states, behavior and they are real-world entities. Collection of all these similar kinds of objects results in phone class, which is a logical entity and It can’t be touched.                                                                                                                            

Features of OOPs in Java:

There are four main features of Object-Oriented programming language.  These are known as pillars of OOPs Concepts in Java.

  1. Inheritance
  2. Polymorphism
  3. Abstraction
  4. Encapsulation

Inheritance:

Inheritance is the property of taking the features from one class to another class. The features may include variables and methods of a class. The class which takes the features is known as the Base class or parent class or superclass and the class which is acquiring the features is known as a derived class or child class or subclass.

The subclass inherits all of its instance variables and methods defined by the super class and it also adds its own unique elements. Thus we can say that subclass is a specialized version of the superclass.

A class can participate in Inheritance with another class by using the “extends” keyword.

There are five main types of inheritance in Java:

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. Hybrid Inheritance
  5. Multiple Inheritance

Java doesn’t support Multiple Inheritance through class concept but is supported through Interface concept, which we would discuss later in this blog.

Real-time example of Inheritance:

Let us compare the specifications of Redmi Note 8 and its updated version Redmi Note 9

Redmi Note8:                                                             Redmi Note9:

Display:  6.3’’                                                      6.53’’

Battery:  4000mAh 5020mAh

Camera: 48MP 48MP

Android Version:  Android 9.0 Android 10

Processor:  Snapdragon 665 Helio G85

Programmatically, Redmi Note8 is a Base class and Redmi Note9 is a Child Class.  The specifications that are mentioned are nothing but properties or state of the object Redmi Note8 and Note9. We can clearly observe the updates from Base Class to the Child class.

Polymorphism:

Polymorphism in Java is a concept by which we can perform a single action in different ways. As the name itself indicates that Poly means many and morphs means forms. There are two types of polymorphism in Java:

  • Method Overloading in Java: Whenever the same method name is existing multiple times within a single class, with a different number of parameters or different types of parameters or different order of parameters, then it is known as Method Overloading in Java.
  • Method Overriding in Java: Whenever the same method name is existing in both base class and derived class with the same type of parameters, the same number of parameters, and the same order of parameters then it is known as Method Overriding in Java.

Real time example of Polymorphism:

If a person is at home, then his task of handling responsibilities is as a son, If the same person is at college, then his task of handling responsibilities is as a student and if the same person is at a company then his task of handling responsibilities is as an Employee. So based on the parameter he gets i.e. either home or college or company, he is handling his responsibilities differently. So a single person can person a single task in different forms.

Abstraction:

Abstraction is a process of hiding the implementation details and showing only functionality to the user or it shows only essential things to the user and hides the internal details. There are two ways to achieve abstraction in Java.

  1. Using Abstract class
  2. Using Interface concept

By Using the Abstract class we can achieve 0-100 percent of abstraction and by using the interface we can achieve a total of 100 percent abstraction.

Real time example of Abstraction:

In every webpage, we would see the functionality but we don’t know how they get. All the backend code i.e. implementation is abstracted away from the user. 

Encapsulation:

Encapsulation is a process of binding private instance variables with the getters and setters method such that data security could be enhanced. A typical encapsulated class should contain all the private variables. So through encapsulation, we provide security to these private variables using getter and setter methods.  

This brings us to the end of the blog on OOPs Concepts in Java. We hope that you have gained valuable knowledge from the same! Happy Learning.