fb

Ads

Pages

Inheritance [VB.Net]

When inheritance was first introduced into some other languages, it caused problems as users went too far, and had very deep inheritance trees. Inheritance is powerful and useful, but should not be overdone. I remember working with  developer. she spent an entire day working on the design of objects and finally said, “I can not figure out where to use polymorphism.” My suggestion was that if he couldn’t find where to use it, maybe she didn’t need to.


There are actually three ways to build an object model:Encapsulation -If you want to use functionality from an object, but not really look like the object or override its functionality, don’t inherit; instead encapsulate an instance of the object. Example: You have a calculator application, and you want to show the number display in red when it is negative. You could define a new type of control from text box or label that shows the number in red when negative, or you can just hook onto the text changed event of the text box, and perform the functionality there. The former mechanism would be overkill.

Interface implementation -If the derived objects don’t share very much interms of implementation, use an interface instead of a common base class.
There is not much point in having a base class for which all the methods must
be overridden; an interface would probably be a better way to go. Classes can
also implement multiple interfaces, so a car may implement both the GasPowered and the Drivable interfaces, whereas an airplane might implement
the GasPowered and the Fly interfaces. A bird, on the other hand, implements
EatWorms and Fly, but does not implement GasPowered or Drivable.
 

Inheritance -Use inheritance if you want to utilize functionality in the base
class and extend it while trying to look like the base class. If some of the methods in the base class are not appropriate for your derived class, that is probably a good indication that inheritance was not the right move. In some cases, merely encapsulating another class instance and proxying calls to it might be a cleaner solution.

0 comments:

Post a Comment