Object-Oriented Programming in Java

Tusharchavhan
5 min readMay 9, 2021

In this article, we learned about the oops concept in java.

Fig:- Oops In Java

As the name suggests the object-oriented programming language deals with the object and classes. the main aim of object-oriented programming is to implement real-world entities like Abstraction, Polymorphism, Encapsulation, and Inheritance.

Object:- Object is a real-world entity it exists in the world. In java, we can create many objects in a class. An object has memory, state, behavior, and Identity. e.g.Maruti Suzuki, Pizza.

Fig:-Object creation in java

In Java, we can create a new object using a new keyword.

Class:- Class is not a real-world entity. It does not have any memory. The class contains many objects.

Fig-Example of Class and Object

In Java, a class can be created using the class keyword. We have an abstract class also present in java it will be declared using abstract keyword.

Abstraction:- Data abstraction is a process of showing only essential things (functional things) and hiding details (internal working).

In java, we achieved abstraction using an abstract class or interface. In abstract class, we can achieve 0–100% abstraction and by using Interface we can achieve 100% abstraction.

Let’s see some real-time examples of abstraction

Car:- In a car, we know the functionality of breaks and accelerators. In breaks, we know that if we press the brake, the car stops, but we don’t know the internal mechanism of breaks. In the accelerator, we know that after pressing the accelerator the speed of the car is increased but how the speed was increased we don’t know that. That is the concept of abstraction.

ATM:- In ATM we know only how to withdrawal money but we don’t know how it actually works internally what is the internal working of that machine.

A. Abstract class: In java, we used abstract class to achieve 0–100% abstraction. An abstract class can contain abstract as well as non-abstract methods. An abstract class can be declared using the abstract keyword in the class signature. And to declare the abstract method we use the abstract keyword in the method signature. We can not create an object of the abstract class. And for inheriting purposes we use extend keyword. if we extend(inherited) any abstract class then the child class has the responsibility to implements all abstract methods.

B.Interface:- In java, to achieve 100% abstraction we used interface. Basically, the interface is a blueprint of a class in which all the methods are abstract and all the variables are final, static, and public. We can not create an object of interface and for inheriting purposes we used implements keyword. we can achieve multiple inheritances by using the interface.

Encapsulation:- Encapsulation is the process of wrapping data members, methods into a single unit, for eg. a capsule in which all mixture of medicines is wrapped into a single unit.

Fig: Reference image for Encapsulation

The Encapsulation class is also called as POJO class. To achieve encapsulation we make all data (variables and method ) private and use the getter-setter method to access those data members. The getter method is used to fetch the data and the setter method is used to set the data in a variable.

Polymorphism:-Polymorphism word comes from the Greek word polymorphic in which poly means many morphic means forms so Polymorphism is nothing but the many forms for the same method. In java, 2 types of polymorphism exist which are as follows.

  1. Compile-time polymorphism:- Complilde-time is also called method overloading and static polymorphism. In a class if “two or more methods have the same name but the different number of parameter or different type of parameter” present then it is called Compile-time polymorphism. A real-time example of Notepad, In notepad, if we press the “A” key then it prints the character A and if we press ctrl+A then it's select all the data. Also if we press the “Z” key then it displays the “Z” character but if we press ctrl+Z then it will be performed as a undo operation. In this example, the Notepad is a class and the key is a parameter to be passed to the method.

Fig:- Reference image for compile-time polymorphism

Fig:- Reference image for compile-time polymorphism

2.Runtime Polymorphism:- Runtime Polymorphism is also called a method overriding and Dynamic polymorphism. If two or more classes present the same method name as well as the same data members with the same type then it is called runtime time polymorphism. Let see a real-time example of runtime polymorphism. Tv remote, In tv remote, if we press the volume on home then it will work to increase or decrease volume, and if we to setting and press the volume key then it will increase or decrease the brightness of tv screen. Here the Home is one class and volume key is a parameter and the setting is another class. The volume key in the home class works to change volume and in setting class works to change the brightness of tv screen.

Fig:- Reference image for Runtime Polymorphism

Inheritance:- Its is the most important property present in oops. Inheritance means it acquires the property and data member of the parent class. It is used for code reusability. For inheriting purposes we used extend keywords like Class A extend B.

Types of inheritance present in java:-

  1. Single inheritance:- In this, if B class inherits the A class then that type of inheritance is called single inheritance.
Fig:- Single Inheritance

2.Multilevel Inheritance:- In Multilevel Inheritance we have multiple single inheritances. The following picture shows the Class 2 extend class 1 and class 1 is extends the superclass.

Fig:- Multilevel Inheritance

3.Hierarchical Inheritance: In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. In the below image, class A serves as a base class for the derived classes B, C, and D.

Fig:- Hierarchical Inheritance

--

--