Java OOPs concepts with example

Java OOPs concepts with an example.



To learn Java the basic requirement or you can say the pillar of java is to learn or to know about Java OOPs Concepts. This is Java Core basic, here you will see the OOps concepts with easy explanation and with an example.

Here first comes to mind what is an OOPs concept in java??

OOPs means Object Oriented Programming language used in modern programming like java. And it is the Programming concept in java that explains the Abstraction, Encapsulation, Inheritance, and polymorphism.

OOPs allows users to create the objects that they want to use and then create those methods to handle those objects. 

Java is known as a truly object-oriented language. It is fully object-oriented, but still, primitive types are used as primitive for simplicity though in java we have Wrapper classes to use primitive types as objects. 

The basic concept of OOPs is to create objects, re-use them throughout the program, and manipulate these objects to get results.


OOPs Concepts:

1. Abstraction

Hiding the implementation and showing the function definition to the user is known as AbstractionAn abstraction is an act of representing essential features without including background details. It is a technique of creating a new data type that is suited for a specific application. For example, while driving a car, you do not have to be concerned with its internal working. Here you just need to concern about parts like steering wheel, Gears, accelerator, etc.

2. Encapsulation

The concept of binding the data along with its related and corresponding method is known as Encapsulation. Encapsulation is an OOP technique of wrapping the data and code. In this OOPS concept, the variables of a class are always hidden from other classes. It can only be accessed using the methods of their current class. For example - in school, a student cannot exist without a class.

3. Inheritance

Inheritance is an OOPS the concept in which one object acquires the properties and behaviors of the parent object. It is also known as IS-A Relationship. It is creating a parent-child relationship between two classes. It offers a robust and natural mechanism for organizing and structure of any software.

4. Polymorphism

Defining multiple methods with the same name is known as Polymorphism. Polymorphism refers to the ability of a variable, object, or function to take on multiple forms. For example, in English, the verb run has a different meaning if you use it with a laptopa foot race, and a business. Here, we understand the meaning of run based on the other words used along with it. The same also applied to Polymorphism.

Two types of Polymorphism:

   1). Static Polymorphism

   2). Dynamic Polymorphism

5. Association

Association is a relationship between two objects. It defines the diversity between objects. In this OOP concept, all object have their separate lifecycle, and there is no owner. For example, many students can associate with one teacher while one student can also associate with multiple teachers.

6. Aggregation

In this technique, all objects have their separate lifecycle. However, there is ownership such that child objects can’t belong to another parent object. For example, consider the class/object department and teacher. Here, a single teacher can’t belong to multiple departments, but even if we delete the department, the teacher object will never be destroyed.

7. Composition

A composition is a specialized form of Aggregation. It is also called a "death" relationship. Child objects do not have their lifecycle so when the parent object deletes all child objects will also delete automatically. For that, let’s take an example of House and rooms. Any house can have several rooms. One room can’t become part of two different houses. So, if you delete the house room will also be deleted.

Advantages of OOPs

1. OOP offers code reusability, method overloading, and method overriding concept which reduces and makes the code readable and easy to understand.

2. OOP offers easy to understand and a clear modular structure for programs.

3. Objects created for Object-Oriented Programs can be reused in other programs. Thus it saves significant development costs.

4. Large programs are difficult to write, but if the development and designing team follow the OOPS concept then they can better design with minimum flaws.

5. It also enhances program modularity because every object exists independently.

That is all OOPs concept with advantages.


Recommended article:

3.Abstraction concept with Program explanation
4.Encapsulation concept in detail with Program explanation
5.Polymorphism concept with Program explanation








Previous
Next Post »