InterviewSolution
| 1. |
Explain the following in the context of Object-Oriented Programming language Java: Coupling, Cohesion, Association, Aggregation, Composition. |
|
Answer» 1. Coupling: Coupling is a term used in object-oriented design to describe the degree of direct knowledge that one element has of another. To put it another way, how often do changes in class A CAUSE changes in class B? Coupling can be divided into two categories:
Let us understand it with the help of the following code: class Volume { public static void main(String args[]) { Box b = new Box(1,2,3); System.out.println(b.volume); }}class Box { public int volume; Box(int length, int width, int height) { this.volume = length * width * height; }}Output: 6
2. Loose Coupling: Class A and Class B are said to be loosely connected if the only knowledge that class A has about class B is what class B has revealed through its interface. SPRING framework employs dependency injection technique with the help of POJO (Plain Old Java Object) /POJI (Plain Old Java Interface) model to overcome the challenges of tight coupling between objects, and it is possible to achieve loose coupling through dependency injection. For instance, if you change your clothes, you are not obligated to change your body - if you are able to do so, you have loose coupling. Tight coupling occurs when you are unable to do so. Interface and JMS are examples of loose coupling. 3. Association: The term "association" refers to a relationship that exists between two distinct classes that are established through their Objects. One-to-one, one-to-many, many-to-one, and many-to-many associations are all possible. An Object communicates with another object in Object-Oriented programming to leverage the capabilities and services provided by that object. The following are the two types of association present: 4. Composition: The strongest sort of relationship is composition. If an item owns another object and the other object cannot exist without the owner object, the association is said to be composition. Take the SITUATION of a human with a heart. The heart is contained within the Human object, and the heart cannot live without the Human. Let us understand it better with the help of the following code: //Car classpublic class Car { //An engine is an integral part of a car private final Engine engine; public Car () { engine = new Engine(); }}//Engine Classclass Engine { //code}Explanation: In the above code, we can clearly see that the two classes share a Composition relationship. The existence of an Engine object is entirely dependent on the existence of a Car object. If no Car object exists, there is no Engine object created and so, the two classes Car and Engine can be said to be in a ‘Composition’ relationship. 5. Aggregation: Aggregation can be referred to as weak association. If both Objects may exist independently, the association is considered to be aggregation. It is a unique type of association in which:
Let us consider the following code to understand it better: //Team classpublic class Team { //players can be 0 or more public List players; public Team () { players = new ArrayList(); }}//Player Classclass Player { int id; String name; Player(int id, String name) { id = this.id; name = this.name; }}//Tournament Classclass Tournament{ public static void main(String args[])throws IOException { Team t1 = new Team(); Player p1 = new Player(1, "A"); Player p2 = new Player(2, "B"); t1.players.add(p1); t1.players.add(p2); Team t2 = new Team(); Player p3 = new Player(3, "C"); Player p4 = new Player(4, "D"); t2.players.add(p3); t2.players.add(p4); }}Explanation: In the above code, we can see that the two classes Team and Player form an Aggregation relationship. A player is a part of a team and so a Player object “HAS-A” relationship with a team object. HOWEVER, the classes Team and Player objects can exist independently. Hence, it forms an “Aggregation” relationship. |
|