In: Computer Science
What can you infer about the coupling between two objects (tight, loose, cannot say) from each of the following
1 They can easily be swapped with different implementations of the same class
2 Their interactions are limited and controlled
3 In order to understand one class you to open other to look at the implemenataion
4 They are very highly reliant on each other
We use implementation inheritance to create a subclass that specializes a superclass either by adding some new behavior or by changing existing behavior. we can also use implementation inheritance to remove some existing behavior. Explain how it would be done and discuss the problem with this approah.
In object oriented programming we have to establish relation between classes for inter-operability and acheiving the goal of dividing the program into modules. Thus coupling is used for this purpose. The coupling may be loose coupling or tight coupling.
* Tight coupling: Tight coupling is where the classes are highly dependant on each other. For simplification consider an example as I want to go to office. Here the person can go to office by any means. But now if we specify the means of travel, I want to go to office by train, here we have specified the way of travelling. This is called tight coupling. Tight coupling ensures that the classes are highly dependant on each other. The changes to be made in the classes are difficult as we may have to change the entire code.
* Loose Coupling: The opposite of tight coupling is loose coupling. Loose coupling ensures that the classes are loosly coupled to each other. Consider the above example of I want to go to office. Here if we don't specify the means of transport if becomes loose coupling. As we are not highly dependant on anything. If we specify the means of transport and if that train is not available or late, then we have problem. Hence goal of Oject oriented programming appraoch is to obtain loose coupling.
1) They can be easily swapped with different implementations of the same class: This is the example of loose coupling. The goal of loose coupling is to make things easy. Swapping between objects is the easiest in loose coupling. When a component of loosely coupled sytem is changed then the other component is not affected. Thus swapping with different implementations of same class is easy. On the other hand in tight coupling this is difficult and complex process to achieve.
2)Their interactions are limited and controlled : The interactions in a loosely coupled system are less and limited because of the interface and less dependant nature. Thus this is a feature of loose coupling. But in case of tight coupling the system classes are highly dependant on each other and their interactions have to be maximum because of high dependancies.
3) In order to understand one class you to open other to look at the implemenataion: When we have to look at the implementation of other class to open one class that system is tightly coupled. Because highly dependant class to the other class is the reason.
4) They are very highly reliant on each other: Because of the direct relation of the classes and no intermediate interface in between, the classes are highly reliant on each other. This is also a drawback of tight coupling. In loose coupling the classes are not highly dependant on each other.
* Implementation inheritance: Inheritance is the key property of OOPs programming approach. When one class inherits the properties, members of another class is called inheritance. Inheritance helps in code re-use. The reason is that when we inherit the properties of another class, the length of code to re-type the same functionalities is reduced. We just have to use the extend keyword. Consider the follwing exapmle,
//language java
import java.util.*;
import java.lang.*;
import java.io.*;
// parent class
class Animal{
public void eats(){
System.out.println("Animals eat food");
}
public void print(){
System.out.println("This is the animal class");
}
}
//child class
class Dog extends Animal{
//inherited method eats
//overiden print method
public void print(){
System.out.println("This is the Dog class");
}
//own method of Dog class
public void bark{
System.out.println("The dog barks");
}
}
If we do not use implementation inhertance in this example then,
1) we have to rewrite all the methods of animal class in dog class.
2)length of code increases.
3) redundant code exists.
Thus when we use inheritance then we achieve code-reusability. The parent class methods are automatically inherited in base class. Child class can have its own method. Also we can override the parent class methods and define our own methods. If not overriden the same method of parent class is used.
Some drawbacks of implementation inheritance :
1) Complexity increases as we have n number of classs inherting a class sequentially. Like class B extends A, C extends B, D extends C. as we have this inheritance we have to go through all the classes codes and implementations. Also multiple inhertance is not supported in some languages like java. For example, Class B extends A, but Class B cannot extend any other class. ( Although this can be solved by using interfaces in java). Also if Class B extends A and Class C extends A, we have no relation.
2) We discussed tight coupling above. Inheritance is tightly coupled procedure. If we make changes in Parent class, the changes would be reflected in child class. Also Object of child class calls constructor of base class. Thus they are tightly coupled.