Java OOPs Concepts
Java is an object-oriented language after C, C++ and most of the concepts are similar. Java allows you to implement real-world entities like objects, classes, abstraction etc.
The object can be considered as a real-world entity like pen, car, table, anything. It allows you to create programs that use class and the object. It makes software programming easier by providing some basic concepts.
Some other concepts that are also considered to be used in the object-oriented design-
The object can be considered to be any entity that has state and behaviour. The object can be physical or logical like a pen, table, keyboard etc.
An object is an instance of the class. An object takes up an address and has some memory associated with it. An object can communicate without understanding the underlying code.
A class can be defined as the collection of the objects and is a logical entity. A class is a blueprint with the help of which you can create the object. It does not require any space to create the class.
It means acquiring the properties and the behaviour of the parent class or the object. It allows you to use the code again and again without writing it over. You can achieve run-time polymorphism using inheritance.
It allows you to perform one task in many ways. This concept can be achieved using method overloading and method overriding.
With this concept, you can hide the important details of the code and show that functionality that is required. We can achieve abstraction using the interface and the abstract classes.
When you bind the code and the data together into a single unit, it is considered to be encapsulation. A java class itself is an example of the encapsulation. Java bean has all the members as private thus ensuring the encapsulation.
It refers to when the class depends on each other and having information about each other. If one class has the information of another class in detail then it is a strong coupling. With the help of the access modifiers, we can define the visibility of the classes, methods or any variable. For weaker coupling, interfaces are used.
It refers to the component level that performs a specific task. A strong cohesive method will perform the very well specified task while the weak cohesive method will break the single task into many small tasks. The Java.io package is highly cohesive while the java.util is weak cohesive.
It specifies the object’s relationship. Any object can be associated with a single object or many other objects. Below are the type of association-
- One to one
- One to many
- Many to one
- Many to many
You can achieve an association with the help of the aggregation. Aggregation refers to the relationship of one object that contains another object as its state. It is known as a “has-a” relationship and is considered to be the weak object relationship.
You can achieve an association with the help of the composition. Unlike the aggregation, it represents a strong object relationship between the dependent and the dependent object. In this scenario, the dependent object does not have their individual existence and will get deleted if the parent object is deleted.
Advantages of the object-oriented programming language
- It provides modularity for easy troubleshooting.
- It allows you to reuse the code using the inheritance concept.
- It ensures program flexibility which can be achieved using polymorphism.
- It enables you to solve the problem effectively.