Inheritance Polymorphism in dot net

Inheritance allows you to declare a new class that retains all of the members and functionality of a previously defined class. This enables you to create classes that implement basic, common functionality, and then create specialized subclasses that serve different but related functions. In this lesson, you will learn how to use inheritance to derive new classes from base classes, how to create new implementations of base class members, and how to create abstract base classes.

Inheritance

Inheritance allows you to create several classes that are distinct but share a common functionality. Specialized classes, called derived classes, inherit from a common class, called the base class. A derived class is also said to extend a base class. The base class encapsulates the common functionality that will be present in each derived class, and the derived classes provide additional functionality that is specific to each class.

For example, let's consider a class called Truck. The Truck class provides all of the basic functionality that a car might need: it has the methods to make it go forward, backwards, and turn, and it encapsulates all of the necessary objects to do so. You might create a derived class called PickupTruck that inherits Truck. Because it inherits Truck, it has all of the basic functionality that Truck provides. You could then choose to implement additional functionality specific to the class, such as a Cargo property. PickupTruck could then serve as a base class for an additional derived class—FourWheelDrivePickupTruck.

Polymorphic Behavior in Inherited Classes

Inherited classes generally have an is a relationship with the base class. A FourWheelDrivePickupTruck is a PickupTruck, and a PickupTruck is a Truck. Any instance of a derived class can polymorphically behave as an instance of its base class. Thus, if a method requires a Truck object, you can supply a PickupTruck object. Any derived class may be implicitly converted to its base class. When cast to its base class, any members implemented by the derived class will be inaccessible—only the members of the base class will be available.

Creating Inherited Classes

You can create an inherited class by using the Inherits keyword in Visual Basic .NET or the colon (:) in C#. The following example declares a PickupTruck class that inherits the Truck class.

Classes can only inherit a single base class, but can also implement additional interfaces. If your class implements multiple interfaces in addition to inheriting a class, you must use the Implements keyword immediately after the Inherits keyword (Visual Basic .NET) or list the interfaces after the colon following the base class (C#).

Once an inherited class has been declared, you can implement additional members to add custom functionality to a class.

To declare an inherited class

In Visual Basic .NET, use the Inherits keyword to designate the base class.
In C#, specify the base class after the colon in the declaration line.

Creating Classes That Cannot Be Inherited

At times, you might want to create classes that cannot serve as a base for other classes. For example, you might create a specialized class for use in your components that would not be useful as a base of functionality for other programmers. In these instances, you can mark your class as NotInheritable (Visual Basic .NET) or sealed (C#). The following example demonstrates how to apply the NotInheritable or sealed keyword.

Inherited Members

When you create an inherited class, the new class possesses all of the functionality found in the base class. In addition to adding new members, you can change the implementation of inherited members to suit specialized purposes. Overriding base members allows you to substitute a new implementation of an existing member for the base class implementation. In Visual Basic .NET, you can also shadow base members.

Shadowing base members allows you to obscure a base member and implement a new member with the same name but completely different characteristics including signature, access level, or even member type. C# allows you to hide base class members, which allows you obscure a base member and implement a new member with the same name and signature, but with different other characteristics. Overriding, shadowing, and hiding are discussed in the following sections.

Overriding Base Class Members

When inheriting from a base class, you can provide a different implementation for base class members by overriding them. Overriding allows you to substitute your own implementation for a base class member of the same name. For example, a Car class might have a GoForward method. If a SportsCar class inherited from the Car class, you might want to provide a different implementation of the GoForward method. Only base class methods and properties can be overridden. Member variables and events cannot be overridden.

You can declare a different implementation of a member by using the Over-rides keyword (Visual Basic .NET) or the override keyword (C#). The new implementation must have an identical signature and return type as the member it is overriding, and must have the same access level.

When a member is overridden, the new member is called in place of the member in the base class. This is true regardless of the context in which the member is called. For example, if an instance of an inherited class is cast to its base class and an overridden method is called, the new implementation will be executed, even though the variable is of the base class type. The type of the object, not the variable, determines which member is called.

In order to override a base class member, that member must be marked as Overridable (Visual Basic .NET) or virtual (C#). Members not so marked are considered fixed and are not overridable. The following example demonstrates how to declare an Overridable (virtual) method.

To override a base class member

Ascertain that the member you are attempting to override is Overridable (virtual). If the member you want to override is not overridable, you must hide it instead.

Provide a new implementation of the member using the Overrides (override) keyword.

Shadowing Base Class Members with Visual Basic .NET

In the previous section, you learned how to replace an implementation of a member in a base class with a different implementation of the same member. It is also possible to hide the implementation of a base class member and replace it with a completely new member that might have a different access level, signature, or even be a different type of member. This is called shadowing. In Visual Basic .NET, you use word the Shadows keyword to substitute a new implementation for a base class member.

Hiding Base Class Members with Visual C#

When working in C#, you can obscure a base class member and replace it with a completely new implementation. This is called hiding. When you hide a member, you replace the base class implementation with a new implementation.

The new implementation must have the same signature as the member that is being hidden and be the same kind of member, but it can have a different access level, return type, and completely different implementation. Any method that has the same name as a preexisting method but a different signature is treated as an overload of that method and will not hide the preexisting method. You use the new keyword to hide a base class member .

Maintaining Compatibility with Shadowed or Hidden Members

When you shadow or hide a class member, you hide the base class implementation and create a new implementation that need not have the same characteristics of the base class member. This can have grave implications for interoperating with other objects. If an object calls the MyMethod function from the preceding example, it would expect a return type of String, and when an Integer was returned from the shadowed member instead, an error would result.

You must therefore take great care when shadowing or hiding a member; it should only be done in cases when you are certain compatibility will not be broken.

Although obscured, the base class implementation of members that are shadowed or hidden will still be accessed under certain circumstances. Whether the base class implementation or the new implementation is accessed depends on the type of the variable rather than the type of the object, as in overridden members.

To shadow or hide a member

In Visual Basic .NET, use the Shadows keyword to shadow a member and provide a new implementation. The new implementation can have a different signature, access level, and return type, or it can even be a different type of member.

In C#, use the new keyword to hide a member and provide a new implementation. The new implementation must have the same signature and be the same type of member but can have a different access level or return type.

Accessing Base Class Members

When creating overridden or hidden members, you might want to access the implementation of the member in the base class. You can access base members by using the MyBase (Visual Basic .NET) or base (C#) keyword. These keywords provide a reference to the base class implementation and allow you to invoke the members implemented in the base class. The following code sample demonstrates how to invoke a member of a base class.

Protected Members

Access levels of class members were discussed in Chapter 1. To review, Public (public) members are accessible to all parts of an application including external classes. Friend, or internal, members are accessible to members of the local assembly but not to external callers. Private (private) members are available only within the class itself and cannot be accessed from any other callers including inherited classes. There are two other access levels that have not been fully discussed: Protected (protected) and Protected Friend (protected internal).

When the Protected (protected) keyword is used with a class member, the member has the same access visibility to external callers as when the Private (private) keyword is used. The difference, however, is that protected members are accessible by derived classes as well.

A second access level is Protected Friend (protected internal). This access modifier provides the union of Protected (protected) access and Friend (internal) access. Thus, a member that is Protected Friend or protected internal can be accessed by classes in the assembly or by classes that inherit its class, in addition to access from within the class itself.

Abstract Classes and Members

When creating components, you might want to create a base class that provides some invariant functionality but leaves the implementation of other members to inheriting classes. You can accomplish this through the use of abstract classes, which are classes that must be inherited.

Abstract classes are similar to interfaces, but also share many features with classes. An abstract class cannot be instantiated on its own; it must be inherited first. Abstract classes can provide all, some, or none of the actual implementation of a class. Like interfaces, they can specify members that must be implemented in inheriting classes. Unlike interfaces, a class can inherit only one abstract class. Like classes, abstract classes can provide fully implemented members, but unlike classes, abstract classes can also specify members that must be implemented by the inheriting classes.

Creating Abstract Members

An abstract class can implement any members, just as a regular class would. Regular members of an abstract class can either be Overridable (virtual), in which case inheriting classes can create their own implementations of the members as needed, or nonvirtual, and thus have a fixed implementation that will be common to all inheriting members.

Abstract classes can also specify abstract members. An abstract member declaration is very similar to the declaration of an interface member. Only the member type, access level, required parameters, and return type are specified. No details regarding the implementation of the member are defined in the method declaration. Thus, only the interface of the member is specified.

To declare an abstract member, you use the MustOverride keyword in Visual Basic .NET, and the abstract keyword in C#. Abstract members must be declared in abstract classes. If you try to declare an abstract member in a nonabstract class, a compiler error will result.

When a nonabstract class inherits from an abstract class, it must provide an implementation for every abstract member defined by the abstract class. An implementation is provided by overriding the member in the same way you would override a member in any other class.

To declare an abstract class

Use the MustInherit keyword with Visual Basic .NET or the abstract keyword with C# in the class declaration to indicate an abstract class.

Use the MustOverride (Visual Basic .NET) or abstract (C#) keyword with any abstract members that you want to define in your class.

Implement any nonabstract members required by your class.

Related Post

DAY 11 OOPS INTRODUCTION

DAY 12 POLYMORPHISM

DAY 13 INHERITANCE AND POLYMORPHISM

DAY 14 EBUGGING TOOLS IN DOT NET

DAY 15 DEBUG AND TRACE IN CLASSES

DAY 16 UNIT TEST PLAN

DAY 17 EXCEPTIONS IN VISUAL STUDIO

DAY 19 ADO.NET INTRODUCTION

DAY 20 DATA ACCESSING IN DOT NET

DAY 21 DATA BASE OBJECTS


No comments:

Post a Comment