Latest Activity In Study Groups

Join Your Study Groups

VU Past Papers, MCQs and More

We non-commercial site working hard since 2009 to facilitate learning Read More. We can't keep up without your support. Donate.


GDB CS304 <Starting Date> Wednesday, July 10, 2013 {{}} <Closing Date> Friday, July 12, 2013

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.

GDB’s Instructions

 

  1. You need to provide precise and to the point answer, avoid irrelevant details.
  2. Copy from the internet or other student will get zero marks.
  1. You should post your answer on the Graded Discussion Board (GDB), not on the Moderated Discussion Board (MDB). Both will run parallel to each other during the specified time. Therefore, due care will be needed.
  2. You need to use font style as “Times New Roman” and font size 12.
  3. You cannot participate in the discussion after the due date via email.
  4. The GDB will open and close on specified date and time. Please note that no grace day or extra time will be given for posting comments on GDB.

Views: 4705

Replies to This Discussion

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:

  • It is easier to change the interface of a back-end class (composition) than a superclass (inheritance). As the previous example illustrated, a change to the interface of a back-end class necessitates a change to the front-end class implementation, but not necessarily the front-end interface. Code that depends only on the front-end interface still works, so long as the front-end interface remains the same. By contrast, a change to a superclass's interface can not only ripple down the inheritance hierarchy to subclasses, but can also ripple out to code that uses just the subclass's interface.
  • It is easier to change the interface of a front-end class (composition) than a subclass (inheritance). Just as superclasses can be fragile, subclasses can be rigid. You can't just change a subclass's interface without making sure the subclass's new interface is compatible with that of its supertypes. For example, you can't add to a subclass a method with the same signature but a different return type as a method inherited from a superclass. Composition, on the other hand, allows you to change the interface of a front-end class without affecting back-end classes.
  • Composition allows you to delay the creation of back-end objects until (and unless) they are needed, as well as changing the back-end objects dynamically throughout the lifetime of the front-end object. With inheritance, you get the image of the superclass in your subclass object image as soon as the subclass is created, and it remains part of the subclass object throughout the lifetime of the subclass.
  • It is easier to add new subclasses (inheritance) than it is to add new front-end classes (composition), because inheritance comes with polymorphism. If you have a bit of code that relies only on a superclass interface, that code can work with a new subclass without change. This is not true of composition, unless you use composition with interfaces. Used together, composition and interfaces make a very powerful design tool. I'll talk about this approach in next month's Design Techniques article.
  • The explicit method-invocation forwarding (or delegation) approach of composition will often have a performance cost as compared to inheritance's single invocation of an inherited superclass method implementation. I say "often" here because the performance really depends on many factors, including how the JVM optimizes the program as it executes it.
  • With both composition and inheritance, changing the implementation (not the interface) of any class is easy. The ripple effect of implementation changes remain inside the same class.

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....

RSS

© 2021   Created by + M.Tariq Malik.   Powered by

Promote Us  |  Report an Issue  |  Privacy Policy  |  Terms of Service