Question

In: Computer Science

What can you infer about the coupling between two objects (tight, loose, cannot say) from each...

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.

Solutions

Expert Solution

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.


Related Solutions

Question 1: A) What can you infer about Neanderthal social structure and behavior from specimens like...
Question 1: A) What can you infer about Neanderthal social structure and behavior from specimens like the Old Man of La Chappelle-aux-Saints?
what can you say about the connection between the price a monopolist chooses to charge and...
what can you say about the connection between the price a monopolist chooses to charge and whether or not demand is elastic, unitary, or inelastic at that price?
what can you say about the economy, culture and religion of FRANCE? can you say something...
what can you say about the economy, culture and religion of FRANCE? can you say something about it? please give an example for each type. thank you so much. needed for my assignment.
there is a discussion about how confidence intervals can be used to infer facts from your...
there is a discussion about how confidence intervals can be used to infer facts from your sample to the population. Select a variable you are likely to work with in your career, then using the video and the text in this course, answer the following questions: 1. Describe the variable and how it might be used in your career (the example from the video is battery life). 2. Explain why you would need to use a sample to infer something...
Give two examples of forces you exerted on objects today. What factors were different about each...
Give two examples of forces you exerted on objects today. What factors were different about each force you mentioned? How do you calculate the net force of an object? What is the difference between a balanced and unbalanced force? Provide an example of each. True or False? Inertia is a force. True or False? The net force of a balanced force is 0. The more mass an object has, the ____________ inertia it has. Using the picture below answer the...
Give two examples of forces you exerted on objects today. What factors were different about each...
Give two examples of forces you exerted on objects today. What factors were different about each force you mentioned? How do you calculate the net force of an object? What is the difference between a balanced and unbalanced force? Provide an example of each. True or False? Inertia is a force. True or False? The net force of a balanced force is 0. The more mass an object has, the ____________ inertia it has. Using the picture below answer the...
What can you infer about the changes in U.K. pound–euro exchange rate during July and August...
What can you infer about the changes in U.K. pound–euro exchange rate during July and August 2014? Can you think of a reason for the behavior of that exchange rate? If the dollar continues its upward path against the euro, what do you predict will be the consequences for U.S. and European relative inflation rates?
If the MU of a product is high, what can you say about the following: The...
If the MU of a product is high, what can you say about the following: The shape of the price elasticity of demand for that product. How high will the price likely to go?
What is statistical inference? A) a way to infer conclusions about the wider population from sample...
What is statistical inference? A) a way to infer conclusions about the wider population from sample data B) using facts about a sample to estimate the truth about the whole population C) a way to describe the uncertainty and variability inherent in all statistical information D) all of the above
We can infer a statistically significant difference between two population means when a) our estimates of...
We can infer a statistically significant difference between two population means when a) our estimates of the two means are different.        d) the 95% confidence intervals for the two means do not overlap. b) the variances of the two means do not overlap.     e) the 95% confidence intervals for the two means are of different widths. c) the standard deviations of the two means do not overlap.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT