GDB Discussion Topic
Suppose you are working as a software developer in a company. You have been given a task to develop a software system design for some real world scenario. One of the basic activities of any software system design is to establish relationships between classes. Suppose at the stage of creating relationships for some classes, you have to choose either inheritance or composition as these are two fundamental ways to relate classes by achieving feature of code re-usability. Being a developer, for what reasons, would you prefer one technique to the other keeping in mind the reusability factor? Support your answer with solid reasons.
plz start disc
Composition is bst choice
i think inheritance is more powerful tool, because we can reuse most of our code in different situations, also it provides better understandings to develop programs.
no compostion is better than iheritance
But i think composition is better than inheritance because Inheritance exposes a subclass to details of its parent's implementation, it's often said that 'inheritance breaks encapsulation'
plz some one tell me keh is hum ny inheritance aur compostion me ju more powerfull hai us ku explain kerna hai mean keh ju is senerio me more sutible hai .ya phir hum ny reusebility per imfessed
kerna hai? plz some one ans me
not power full but keep in mind the reuse-ability. inherited can be reuse easily where as in composition objects destroyed with the parent objects distructor.
personally i learned to always prefer computition over inharitance.
with compusition, it's easy to change behaviour on the fly with dependency setters. Composition is often more logical then inharitance, it provide better abstruction,better encapsulation,better code reuse and is less likely to break anything at the distence just becouse you made an isolated change anyware in the code . it is easir to change the interface of a back-end and front-end then a superclass(inharitance)
Comparing composition and inheritance
So how exactly do composition and inheritance compare? Here are several points of comparison:
qaiser abbas thanks for sharing .keep it up
Inheritance is the clearest solution if and only if the classes are naturally born to have this kind of relation. For sample, it is natural to derive a class A4_Sheet from a class Sheet but it's not so natural to derive a class Document from a class A4_Sheet or Sheet. Maybe in some context Document class can inherit from Sheet but it is more natural to pass this object to, let's say, a function Document:: Draw() or have an array (or a collection) of Sheets inside the Document class.
So there's not a single answer but it strongly depends from your population of classes.
I think Inheritance is more powerful technique regarding code re-usability.As the name suggests Inheritance is the process of forming a new class from an existing class that is from the existing class called as base class, new class is formed called as derived class. Inheritance helps to reduce the code size....