Question:
What is the difference between composition and inheritance?
Answer:
Composition is the technique of combining the simpler data types with the complex data types e.g. function calls within the calling function.<br>Inheritance is one, in which one can acquire the property of other.. Source: CoolInterview.com
Answered by: vinod rajput | Date: 12/14/2007
| Contact vinod rajput
Composition & inheritance is totally different from each other composition means compose the properties by code of same class in composition we can refer some other class who hold same properties but we have to code we can not directly use that code ,we have to rewrite .<br><br>But inheritance i.e inherit the properties of other class .in it method of class to whom we inherit can directly call by the child class object.<br><br>if we think as real life example if we inspired by someone we adope the properties of that person we have to add that quality of person ourself ,that is composition. but some quality of our parent automatically in us there is no need to strive ,that is inheritance Source: CoolInterview.com
Answered by: Deepak jain | Date: 12/19/2007
| Contact Deepak jain
Let me be a bit elaborate. There are two kinds of inheritances. One if of the kind "IS A" and the other is of the kind "HAS A". "IS A" type of inheritance is like the following<br><br>class car:public vehicle<br>{<br>}<br>where it is said CAR "IS A [KIND OF ]" VEHICLE.<br><br>There are two kinds of "HAS A" inheritance implementation. Aggregation and Composition. <br><br>Now consider the following<br><br>class Heart<br>{<br>}<br><br>class HumanBeing<br>{<br>private :<br>Heart _heart;<br>}<br><br>The above example is that of "COMPOSITION" type of "HAS A" Inheritance. The Human Being "HAS A" heart and human being can not live with out heart. There is no sense of the lifecycle of human without heart ( one verson of the academia ) and no lifecycle of heart without human ( another version of the academia ). So if one destroys human, the heart also gets destroyed. This is signified as "COMPOSITION".<br><br>Consider the following<br><br>class Sheep<br>{<br>}<br><br>class HerdOfSheep<br>{<br>private Sheep *_pSheep;<br>}<br><br>In the above case the HerdOfSheep can be empty and sheep can exist without being in the herd. This kind of "HAS A" relationship in inheritance is called as "AGGREGATION". The scope of sheep is even outside the scope of the herd.<br><br>-Parthasarathy Source: CoolInterview.com
Answered by: Parthasarathy | Date: 12/27/2007
| Contact Parthasarathy
Inheritance is a "Is A" relationship and composition is "Has A" relationship. Composition is for code reuse. Inheritance is not for code reuse but for flexibility. Source: CoolInterview.com
Answered by: Nagendra | Date: 1/7/2008
| Contact Nagendra
Composition - A class has the object of class B i.e we can access the member variable of class B through class A also.<br>ex. <br>Class B<br>{<br>}<br>Class A<br>{<br>A a;<br>}<br><br>Inheritance : If we say that class A inherit from Class B that means Class B has the characteristics of Class A (depending on the access modifier).<br> Source: CoolInterview.com
Answered by: Amit | Date: 1/9/2008
| Contact Amit
We can say private inheritance is a syntactic variant of composition (has-a).<br><br>E.g., the "Car has-a Engine" relationship can be expressed using composition:<br><br>class Engine {<br>public:<br>Engine(int numCylinders);<br>void start(); // Starts this Engine<br>};<br><br>class Car {<br>public:<br>Car() : e_(8) { } // Initializes this Car with 8 cylinders<br>void start() { e_.start(); } // Start this Car by starting its Engine<br>private:<br>Engine e_; // Car has-a Engine<br>};<br><br>The same "has-a" relationship can also be expressed using private inheritance:<br><br>class Car : private Engine { // Car has-a Engine<br>public:<br>Car() : Engine(8) { } // Initializes this Car with 8 cylinders<br>Engine::start; // Start this Car by starting its Engine<br>};<br><br>There are several similarities between these two forms of composition:<br>* In both cases there is exactly one Engine member object contained in a Car<br>* In neither case can users (outsiders) convert a Car* to an Engine*<br><br>There are also several distinctions:<br>* The first form is needed if you want to contain several Engines per Car<br>* The second form can introduce unnecessary multiple inheritance<br>* The second form allows members of Car to convert a Car* to an Engine*<br>* The second form allows access to the protected members of the base class<br>* The second form allows Car to override Engine's virtual[20] functions<br><br><br>Use composition when you can, private inheritance when you have to.<br><br>Normally you don't want to have access to the internals of too many other<br>classes, and private inheritance gives you some of this extra power (and<br>responsibility). But private inheritance isn't evil; it's just more expensive<br>to maintain, since it increases the probability that someone will change<br>something that will break your code.<br><br>A legitimate, long-term use for private inheritance is when you want to build a<br>class Fred that uses code in a class Wilma, and the code from class Wilma needs<br>to invoke member functions from your new class, Fred. In this case, Fred calls<br>non-virtuals in Wilma, and Wilma calls (usually pure virtuals[22.4]) in itself,<br>which are overridden by Fred. This would be much harder to do with<br>composition.<br><br>class Wilma {<br>protected:<br>void fredCallsWilma()<br>{<br>cout << "Wilma::fredCallsWilma()<br>";<br>wilmaCallsFred();<br>}<br>virtual void wilmaCallsFred() = 0; // A pure virtual function[22.4]<br>};<br><br>class Fred : private Wilma {<br>public:<br>void barney()<br>{<br>cout << "Fred::barney()<br>";<br>Wilma::fredCallsWilma();<br>}<br>protected:<br>virtual void wilmaCallsFred()<br>{<br>cout << "Fred::wilmaCallsFred()<br>";<br>}<br>};<br><br><br> Source: CoolInterview.com
Answered by: Richa gupta | Date: 1/10/2008
| Contact Richa gupta
I too like to know the answer. Source: CoolInterview.com
Answered by: Prabhuraj | Date: 1/13/2008
| Contact Prabhuraj
Inheritance:- Two class can exhibits an IS_A relationship called inheritance.<br><br>Composition:- This concept comes under the containment(in which one(outer) object holds another(Inner) object). This shows the HAS_A relationship. In this type of containment the outer object controls the life time of the inner objects. e.g (outer)[Bank] HAS_A [Account](inner). Source: CoolInterview.com
Answered by: Jitendra singh | Date: 1/26/2008
| Contact Jitendra singh
Composition is nothing but the Has-A relationship which is indicates inner class is not independent from outer class .<br><br>This is creates the COHESION and dependencies and which is tightly coupled relationship and this is not recommended many times in OOPS .<br> Source: CoolInterview.com
Answered by: Ashwinkumar | Date: 1/26/2008
| Contact Ashwinkumar
Defining a class as a member of another class is composition.<br><br>A process by which object of one class acquire the properties of another class is inheritance. Source: CoolInterview.com
Answered by: Manoj Jangid | Date: 2/2/2008
| Contact Manoj Jangid
java is object oriented program but another one is not. Source: CoolInterview.com
Answered by: tamil | Date: 2/5/2008
| Contact tamil
In a Class composition <br>the use of the class has clear knowledge of all the classes which are used in the composition. the classes appear as data members. <br><br>In inheritance the base class is totally "dismantled" and the use of the class has knowledge of the base class. all the methods and data members of the base class is the member of the new derived class. this is different from the above case in which the composing class it self appears as data member. Source: CoolInterview.com
Answered by: Amaren | Date: 4/3/2008
| Contact Amaren
Composition means Creating object of one class in another class.<br><br>Inheritance means deriving a new class from already existing class. Source: CoolInterview.com
Answered by: Ravikumar Horatti | Date: 7/27/2008
| Contact Ravikumar Horatti
From a Desing Perspective Favour Composition over Inheritance.<br>(1) Inheritance favours tight coupling.<br>(2) Compisition favours loose coupling.<br>(3) Inheritance: Any changes in the base class might have ripple effect in the derived class.<br>(4) Inheritance: Favours weak encapsulation.<br>(5) Composition: favours strong encapsulation.<br>(6) Inheritance is achieved by Extension.<br>(7) Composition is ahcieved by Instance variable.<br>(8) Inheritance is static binding, where as composition is dynamic binding.<br><br>As per design principle:<br>A class should be closed for modification and open for extension.<br> Source: CoolInterview.com
Answered by: rakshith | Date: 12/7/2009
| Contact rakshith
Composition means a class which is composed of other class and it is called has a relation...e.g Car has an engine, has tyre etc.<br>here is an example of composition where class Y is composed of class X<br><br>'''''''''''''''''<br>Class X<br>{<br>private:<br>int i;<br><br>X(){i=0;}<br>void Set(int ii)<br>{<br>i=ii;<br>}<br><br>int Get()<br>{<br>return i;<br>}<br><br>void improve(int ii)<br>{<br>return i+=ii;<br>}<br><br>};<br><br>Class Y<br>{<br>private:<br>int i;<br><br>public:<br>X x; //emeding obj of X<br><br>void Set(int ii)<br>{<br>i=ii;<br>}<br><br>int Get()<br>{<br>return i;<br>}<br>};<br><br>int main()<br>{<br>Y y;<br>y.Set(5);<br>y.x.Set(5);<br>} Source: CoolInterview.com
Answered by: Danish Afzal | Date: 5/24/2010
| Contact Danish Afzal
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|