i

JAVA Complete Course

Object Oriented Paradigms

In Object Oriented Programming there are three main principles (Encapsulation, Inheritance, Polymorphism) but also we have some paradigms, such as: Coupling, Cohesion, Association, Aggregation and Composition. We will touch the main OOP principles a bit later but let’s now shortly revisit the paradigms.

Coupling refers the knowledge of dependencies between classes. Loosely coupled objects, which are desired in Java application, means that objects should be as less dependent to each other as possible. If a class contains the details of another class, it is said that they have strong coupling. We can use interfaces for the weaker coupling because there is no concrete implementation.

Cohesion defines the component which performs a single well-defined task. Highly cohesive method is a guarantee of being done a single well-defined task. The weakly cohesive method will split the task into separate parts. In Java library, one of the highly cohesive package is java.io because it has I/O related classes and interfaces. One of the example of weakly cohesive package is java.util package because it has unrelated classes and interfaces.

Association refer the relationships between classes. There are four types of association:

  • One to One – one class has only one associated object. For example, Country and President. One country has only one president.

  • One to Many – one class has more than one associated objects. For example, Department and Employees, one department has more than one (many) employees.

  • Many to One – many classes has one associated class. For example, Minister and Prime Minister, many ministers have one prime minister in each country.

  • Many to Many – many classes have many associated classes. For example Minister and Department, many ministers can have many departments.

One of the way to achieve Association is Aggregation. It represents the relationship where one object contains other objects as a part of its state. Aggregation represents the weak relationship between objects.

Composition is another way to achieve Association. It represents the relationship between two objects where one contains another as a part of its state. Between the containing object and the dependent object is a strong relationship. Composition is the state where containing objects don’t have an independent existence. If you delete the parent object, all the child objects will be deleted automatically. 

Abstraction is nothing than hiding internal details of any class and showing only the functionality. For example, we have a method which is performing bank transactions. We are exposing this method to users as an interface method, but the actual implementation is hidden for everyone, this is abstraction. In Java, to achieve the abstraction we use abstract classes or interfaces.