google ads

Tuesday, May 19, 2009

C SHARP BOOK

Inheritance
Inheritance is a fundamental feature of an object-oriented system, and it is simply the ability to inherit
data and functionality from a parent object. Rather than developing new objects from scratch, new code can be based on the work of other programmers[1], adding only the new features that are needed.
The parent object that the new work is based upon is known as a base class, and the child object is known as a derived class.
Inheritance gets a lot of attention in explanations of object-oriented design, but the use of inheritance isn’t particularly widespread in most designs. There are several reasons for this.
First, inheritance is an example of what is known in object-oriented design as an “is-a” relationship. If a system has an animal object and a cat object, the cat object could inherit from the animal object, because a cat "is-a" animal. In inheritance, the base class is always more generalized than the derived class. The cat class would inherit the eat function from the animal class, and would have an enhanced sleep function. In real-world design, such relationships aren’t particularly common.
Second, to use inheritance, the base class needs to be designed with inheritance in mind. This is important for several reasons. If the objects don’t have the proper structure, inheritance can’t really work well. More importantly, a design that enables inheritance also makes it clear that the author of the base
class is willing to support other classes inheriting from the class. If a new class is inherited from a class where this isn’t the case, the base class might at some point change, breaking the derived class.
Some less-experienced programmers mistakenly believe that inheritance is “supposed to be” used widely in object-oriented programming, and therefore use it far too often. Inheritance should only be used when the advantages that it brings are needed[2]. See the coming section on “Polymorphism and

Virtual Functions.”
In the .NET Common Language Runtime, all objects are inherited from the ultimate base class named object, and there is only single inheritance of objects (i.e., an object can only be derived from one base class). This does prevent the use of some common idioms available in multiple-inheritance systems such as C++, but it also removes many abuses of multiple inheritance and provides a fair
amount of simplification. In most cases, it’s a good tradeoff. The .NET Runtime does allow multiple inheritance in the form of interfaces, which cannot contain implementation. Interfaces will be discussed

No comments:

Post a Comment