Sitemap

# OOPS in JAVA

3 min readSep 11, 2022

--

Object oriented programming is about creating objects that contain both data and methods. In simple Object is as a data for datatype as class. OOPS implement real-world entities. Abstraction, Encapsulation, Inheritance and Polymorphism help in Implementing OOPS paradigm.

* Following are the advantages of OOPS over POP:

* It makes the code easier to maintain, modify and debug.

* Make clear structure for the program.

* It Avoid repetition of code.

* Following are the concepts of OOPS:

* Object

* Class

* Inheritance

* Polymorphism

* Abstraction

* Encapsulation

## Object in Java

An object is an instance of a class and which has its own **State**, **behavior**, **identity**. It is created for a class at run time and represent real-world entity.Whenever an object is created, they inherit variables and method from class.

* **State**: It represents value of an object.

* **Behavior**: It represents functionality of an object.

* **Identity**: It is unique for each object.

## Class in Java

Class is blueprint or template for which object is created and it is defined by the user. Its Properties or method can be shared by multiple object of it, It reduce the multiple times writing of same set of code. A class may contains following components:

* **Modifiers**: When a class is declared out side of any class then it have only two modifiers, public and default.

* **Class name**: By convention class name should be begin with capital letter.

* **Constructors**: A special method, Which is use to initialize object.It is called when an object is created,it doesn’t have any return type. It is of 3 type default constructor, parameterized and non-parameterized constructor.

* **Super class**: A class may extend another class but should be one only.

* **Interfaces**: A class may implements any number of interface.

* **Body**:Body of class is surrounded by braces,{}.

## Pillars of OOPS Concept

### Abstraction

Get Shahazada Talib’s stories in your inbox

Join Medium for free to get updates from this writer.

It reduce the complexity for the user by hiding internal complex process and just showing simple interactive things for the user.

Abstraction can be implemented in two ways in java, One is Abstract class and another is Interface.

* **Abstract Class :** It is declare with abstract key word and it cannot be instantiated and It can have abstract and non abstract method both. It can only extends by another class.

* **Abstract Method :** It can only be declare in abstract class.It does not have any body. It should be define in derived class or declare abstract in derived class.

### Encapsulation

Combining of member function and data member together is in a class is called encapsulation. We can achieve encapsulation by delcaring data member’s access modifier as private and uses getter and setter method to access the value and set the value to the variable.

* It increase the security

* make the attribute as read-only or write-only.

* enchanced the control over method and attribute of class.

### Inheritance

A class which inherit member function and data member from another class is called derived class and from which method and data is inherited is called base class and the process is called inheritance.

* It increase the reusability of the code by using the same set of code by both parent and child class.

* It use in achieving Method overriding or runtime polymorphism.

* In java we use extends keyword to inherit data member and member function from super class or base class.

* It create IS-A relation between two class.

* Inheritance is of four types in Java

* 1. Single Inheritance

* 2. Hybrid Inheritance

* 3. Multilevel Inheritance

* 4. Hierarchical Inheritance

* 5. Multiple Inheritance

* Multiple Inheritance is only achieved by Interface not by class.

### Polymorphism

A function with same name perform different action is called polymorphism, It shows multiple form of single function.

There are two type of polymorphism, One is function overloading or compile time polymorphism and another is called runtime polymorphism or function overriding.

* **Runtime Polymorphism :** When a function from super class is called in derived class and redefine its body is called runtime polymorphism or dynamic polymorphism.

It can’t be achieved by data member.

* **Compile-time Polymorphism :** It is achieved by function overloading and Operator Overloading. But Java does not support Operator overloading. When there are multiple function having same name but different number of parameter or different in data type of parameter or different in return type of function then these function are said to be overloaded.

--

--