Question

In: Computer Science

Explain why composition is generally preferred to inheritance for achieving code reuse.

Explain why composition is generally preferred to inheritance for achieving code reuse.

Solutions

Expert Solution

Before talking about the inclination of Composition over Inheritance for accomplishing code reusability, let me give you a brief idea about Composition and Inheritance.

Composition have "HAS-A" relationship. During this an object owns another objects instead of just use that object. It basically means if the main object are going to be destroyed all the interior objects owned by main object should be destroyed also. Example: during this example, class BMW is said to class Car by composition, because BMW has an instance variable that holds a regard to a Car object.

class Car {
//...
}
class BMW {
public Car car1 = new Car();
//...
}

Inheritance have "IS-A" relationship. it's used for establishing the relationship between different classes. Subclass may inherit the properities and methods of its superclass. Superclass have rights to override some methods and properities to switch the default behaviour of parent class. Example: during this simple example, class BMW is associated with class Car by inheritance, because BMW extends Car. during this example, Car is the superclass and BMW is the subclass.

class Car {
    //...
}
class Car extends BMW {
    //...
}

The reasons are as follows:

  1. Composition is more flexible than Inheritance. you can change implementation of class at run-time by changing included object, thus changing behavior of it, but you cannot do that with Inheritance, you cannot change behavior of base class at run-time.
  2. Composition offers better test-ability of a category than Inheritance. If one class consists of another class, you'll easily create Mock Object representing composed class for sake of testing. Inheritance doesn't provide this luxury, so as to check derived class, you want to need its super class. Since unit testing is one among the foremost important thing to think about code reuse during software development, especially in test driven development, composition wins over inheritance.
  3. Composition allows code reuse even from final classes whereas Inheritance cannot extend code reuse final class.
  4. If you employ Composition you're flexible enough to exchange implementation of Composed class with better and improved version.

Related Solutions

Explain why composition is generally preferred to inheritance for achieving code reuse.
Explain why composition is generally preferred to inheritance for achieving code reuse.
Explain why firms choose to decentralize and give an example Explain why NPV is generally preferred...
Explain why firms choose to decentralize and give an example Explain why NPV is generally preferred over IPR when choosing among competing or mutually exclusive project. Why would managers continue to use IRR to choose among mutually exclusive projects?
Describe and contrast the features of common stock and preferred stocks. Explain why dividend payments generally...
Describe and contrast the features of common stock and preferred stocks. Explain why dividend payments generally follow changes in earnings. List of important dates for dividend payments and enumerate the advantages of dividend reinvestment plans. What tenet of investing do dividend reinvestment plans subscribe to? What are the similarities and differences between a stock split and a stock dividend? Why would a company do them?
A- Explain the term "waste reuse" and mention the main difference between waste reuse and waste...
A- Explain the term "waste reuse" and mention the main difference between waste reuse and waste recycle A- Briefly describe what you know about Ozone depletion and Global warming.and Name the major risks caused by a SOx on humans? -What air pollution control device is better in removing SO4? Draw the device and label all the important components. A- List all the three Exposure Pathways to consider in risk assessment. Draw the Inverted Hierarchy of Waste Minimization and give one...
Question: Why is the accrual basis of accounting generally preferred over the cash basis?
Question: Why is the accrual basis of accounting generally preferred over the cash basis?
Reuse the code to prompt the user for a specific day, test whether it is within...
Reuse the code to prompt the user for a specific day, test whether it is within range, and then output the estimated concrete strength from the fitted curve using polyval. Run your function from the command window for 2 different days to test the functioning of your code. The program should preferably loop until the user asks to exit. where x = days y = rain (mm) x = [0 1 2 3 7 14 28]; y = [0 4...
Consider the DataSource class in below code. Explain how inheritance is intended to be used with...
Consider the DataSource class in below code. Explain how inheritance is intended to be used with this class. Does this represent a good use of inheritance? Explain your answer // Base class for game configuration public abstract class DataSource {     private Graph map;     private HashMap <String,Room> rooms;     private ArrayList <Entity> entities;     private Player player;     protected HashMap < String, List<String[]> > tables;     // constructor     public DataSource() {     }     // Connect to the...
Write the code to implement the concept of inheritance forVehicles. You are required to implement inheritance...
Write the code to implement the concept of inheritance forVehicles. You are required to implement inheritance betweenclasses. You have to write four classes in C++ i.e. one superclass, two sub classes and one driver class. Vehicle is the super class whereas Bus and Truck are sub classesof Vehicle class. Transport is a driver class which contains mainmethod. Detailed description of Vehicle (Super class): The class Vehicle must have following attributes: 1. Vehicle model 2. Registration number 3. Vehicle speed (km/hour)...
1. Implement a method that meets the following requirements: (a) Do not reuse any code for...
1. Implement a method that meets the following requirements: (a) Do not reuse any code for the following: i. Try to write this method with as few lines of code as you can ii. Sorts a group of three integers, x,y and z, into decreasing order (they do not have to be in a sequence). iii. Assume the value in x is less than the value in z. You can also assume there are no duplicates among x, y and...
why do companies issue preferred stock, explain why.
why do companies issue preferred stock, explain why.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT