Interface Polymorphism

Interfaces allow you to define contracts for behavior. Different and diverse classes can implement the same interfaces and can thus interact with other objects in a polymorphic manner. In this lesson, you will learn how to define an interface, how to implement an interface, and how objects can interact through an interface.

An interface is a contract. Any object that implements a given interface guarantees to provide an implementation of the members defined in that interface. If an object requires interaction with a specific interface, any object that implements that interface can supply the requisite interaction.

An interface defines only the members that will be made available by an implementing object. The definition of the interface states nothing about the implementation of the members, only the parameters they take and the types of values they will return. The implementation of an interface is left entirely to the implementing class.

It is therefore possible for different objects to provide dramatically different implementations of the same members. Take, for example, an interface named IShape, which defines a single method CalculateArea.

A Circle class that implemented this interface would calculate its area differently than a Square class that implemented the same interface. However, an object that needed to interact with an IShape could call the CalculateArea method in either a Circle or a Square and obtain a valid result.

Interfaces are defined with the Interface (interface) keyword.

The declaration defines the IDrivable interface, but does not define any members. Member methods must be defined with the signature of the method, but without access modifiers such as public, private, and so on.

The access modifier of the interface determines what the access modifier of the interface members will be. Thus, if you have a Public interface, all of the members must be Public as well.

You can also add properties to your interface definitions. A definition for a property must include ReadOnly or WriteOnly when appropriate (in Visual Basic) or define getters, setters, or both (in C#) as well as specify the return type for the property.

Although you can define properties in interfaces, you are not allowed to define fields. This ensures that classes that interact through the interface do not have access to the internal data of an object.

Interfaces can also define events. These represent events that might be raised by objects implementing the interface. Although any class that implements an interface must provide an implementation for any member events, objects that interact through that interface are not obliged to handle any events that are raised.

Although a default delegate type is automatically provided for the event in Visual Basic .NET, you must explicitly designate the delegate type for the event in C#.


To implement an interface with C#

Define a class that implements the desired interface. Use the colon (:) to declare which interface is being implemented in the class declaration. A class can implement multiple interfaces.

Provide an implementation for each member of the interface:

If you want a member to be available to the class and the interface, create a member with the same name, access level, and signature as the member defined by the interface.

If you want a member to be available only to the interface, implement it using the fully qualified interface name. It is not necessary to provide an access modifier.

Implementing Interface Members with Visual Basic .NET

In Visual Basic .NET, you specify that a class member implements an interface member by using the Implements keyword. The class member that implements the interface member must have the same signature as defined in the interface, but does not need to have the same access level.

Any calls to the GoForward method of the interface in this example will be mapped to the Move method of the implementing class.

You can also specify a different access level for a class method that implements an interface method. For example, you can implement a method of a Public interface with a Private class method. If you take this approach, the method will be Public when accessed through the interface, but will remain Private when accessed as a member of the class.

To implement an interface with Visual Basic .NET

Define a class that implements the desired interface. Use the Implements keyword to declare which interface is being implemented in the class declaration. A class can implement multiple interfaces.

Use the Implements keyword to provide an implementation for each member of the interface.

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