Sunteți pe pagina 1din 7

Deccansoft Software Services - MS.NET C# and VB.

NET Polymorphism and Interface

Agenda

1. What is Polymorphism
2. Overview of Interface
3. Interface with examples
4. Types of inheritance

Table of Contents

OVERVIEW 2
TYPES OF INHERITANCE 7

1
Deccansoft Software Services - MS.NET C# and VB.NET Polymorphism and Interface

Overview of Interfaces
An interface is a point of contact with an object.
All public members of a class can be said as an interface to it.
An interface is a collection of related methods and properties without implementation (abstract).
Interface = Pure Abstract Class (A class with all members declared as abstract)
An interface cannot be instantiated.
An interface represents a contract, in that a class that implements an interface must implement every
aspect of that interface exactly as it is defined. The class and Interface are related with Implements
relationship
For some reason if the implementing class cannot provide implementation to any of the interface member,
the member must still be declared in the class but it can be declared as abstract (also making the class as
abstract).
Every member of an interface is by default public and abstract. Interface cannot have field members. i.e. it
can have only procedures (property / methods).
A variable of type interface can refer to an object of any class implementing that interface.
Example:
Create a new Console Application and add the following code to the project.
Example:1
Public Interface IFigure
Property Dimension() As Integer
Function Area() As Double
Function Perimeter() As Double
End Interface
Public Class Circle
Implements IFigure
Private _Radius As Integer
Property Dimension As Integer Implements IFigure.Dimension
Get
Return _Radius
End Get
Set(ByVal value As Integer)
_Radius = value
End Set
End Property
Function Area() As Double Implements IFigure.Area
Return Math.PI * _Radius * _Radius
End Function
Function Perimeter() As Double Implements IFigure.Perimeter
Return 2 * Math.PI * _Radius
End Function
End Class
Public Class Square
Implements IFigure
Private _Side As Integer
Public Property Dimension As Integer Implements IFigure.Dimension
Get
Return _Side
End Get
Set(ByVal value As Integer)

2
Deccansoft Software Services - MS.NET C# and VB.NET Polymorphism and Interface

_Side = value
End Set
End Property
Public Function Area() As Double Implements IFigure.Area
Return _Side * _Side
End Function
Public Function Perimeter() As Double Implements IFigure.Perimeter
Return 4 * _Side
End Function
End Class
Public Class Program
Shared Sub Main(ByVal args() As String)
Dim fig As IFigure = Nothing
If (args(0) = "S") Then
fig = New Square()
End If
If (args(0) = "C") Then
fig = New Circle()
End If
fig.Dimension = 10
Console.WriteLine(fig.Area())
Console.WriteLine(fig.Perimeter())
End Sub
End Class
Code: 6.1 VB

Example:1
using System;
interface IFigure
{
int Dimension { get; set; }
double Area();
double Perimeter();
}
class Circle : IFigure
{
private int _Radius;
public int Dimension
{
get
{
return _Radius;
}
set
{
_Radius = value;
}
}
public double Area()
{
return Math.PI * _Radius * _Radius;
}
public double Perimeter()
{
return 2 * Math.PI * _Radius;
}
}

3
Deccansoft Software Services - MS.NET C# and VB.NET Polymorphism and Interface

class Square : IFigure


{
private int _Side;
public int Dimension
{
get
{
return _Side;
}
set
{
_Side = value;
}
}
public double Area()
{
return _Side * _Side;
}
public double Perimeter()
{
return 4 * _Side;
}
}
class Program
{
static void Main(string[] args)
{
IFigure fig = null;
if (args[0] == "S")
fig = new Square();
if (args[0] == "C")
fig = new Circle();
fig.Dimension = 10;
Console.WriteLine(fig.Area());
Console.WriteLine(fig.Perimeter());
}
}
Code: 6.1 C#

Example:2 -Case:1
Option Strict On 'Important
Public Interface IA1
Sub Foo1()
Sub Foo2()
End Interface
Public Interface IA2
Sub Foo1()
Sub Foo3()
End Interface
Class CA
Implements IA1, IA2
Public Sub Foo11() Implements IA1.Foo1 'Explicitly Implemented with a different name
Console.WriteLine("In IA1.Foo1")
End Sub
Public Sub Foo1() Implements IA2.Foo1 'Explicitly Implemented
Console.WriteLine("In IA2.Foo1")
End Sub

4
Deccansoft Software Services - MS.NET C# and VB.NET Polymorphism and Interface

Public Sub Foo2() Implements IA1.Foo2 'Implicitly Implemented


Console.WriteLine("In Foo2")
End Sub
Public Sub Foo3() Implements IA2.Foo3 'Implicitly Implemented
Console.WriteLine("In Foo3")
End Sub
End Class
Public Class Program
Shared Sub Main(ByVal args() As String)
Dim a1 As IA1
Dim a2 As IA2
Dim a As CA
a = New CA()
a1 = a
a2 = a
'a = a1 'Invalid
a = CType(a1, CA)
a1 = CType(a2, IA1)
a.Foo1()
a.Foo2()
a1.Foo1()
a2.Foo1()
End Sub
End Class
Code: 6.2 VB

Example:2 - Case1 Public Implementation


using System;
interface IA1
{
void Foo1();
void Foo2();
}

interface IA2
{
void Foo1();
void Foo3();
}
class CA : IA1, IA2
{
public void Foo1() //Publicly Implemented
{
Console.WriteLine("In Foo1");
}
public void Foo2() //Implicitly Implemented
{
Console.WriteLine("In Foo2");
}
public void Foo3() // Implicitly Implemented
{
Console.WriteLine("In Foo3");
}
}
class Program
{
static void Main(string[] args)

5
Deccansoft Software Services - MS.NET C# and VB.NET Polymorphism and Interface

IA1 a1; IA2 a2; CA a;


a = new CA();
a1 = a; //Implicit Casting because every object referenced by a (of CA) can also be
referenced by a1
a2 = a; //Implicit Casting because every object referenced by a can also be referenced
by a2
//a = a1; //Invalid because every object referenced by a1 (any class which has
implemented interface
//IA1) cannot also be referenced by a
a = (CA) a1; //Explicit Casting
a1 = (IA1) a2; //Explicit Casting - //an interface variable can never be implicitly casted to
//another interface variable (unless they are related as parent and child).
a.Foo1(); //In case1 prints In Foo1 but in case 2 it compilation error.
a.Foo2();
a1.Foo1(); //In case1 prints In Foo1 but in case 2 it prints In IA1.Foo.
a2.Foo1();
}
}
Code: 6.2 C#

Example:2 - Case2 - Explicit Implementation


class CA : IA1, IA2
{
void IA1.Foo1()//Explicitly Implemented
{
Console.WriteLine("In IA1.Foo1");
}
void IA2.Foo1() //Explicitly Implemented
{
Console.WriteLine("In IA2.Foo1");
}
public void Foo2() //Publicly Implemented
{
Console.WriteLine("In IA1.Foo2");
}
public void Foo3() //Publicly Implemented
{
Console.WriteLine("In IA2.Foo3");
}
}
Code: 6.3 C#

Note: In the project either add code of Case 1 or Case 2

1. Publicly implemented member of an interface in class must be public and must have same name as the
member of interface being implemented by it.
2. Explicitly implemented member cant have any access specifier because it can be never called directly using
the reference variable of the class in which it is member.
CA a = new CA( );
a.Foo1( ) ; //Valid in Case 1
a.Foo1() or a.IA1.Foo1( ) //Invalid in Case 2

6
Deccansoft Software Services - MS.NET C# and VB.NET Polymorphism and Interface

3. If we have a common method in more than one parent interface, different implementations can be provided
in the implementing class by explicitly implementing the interface. Example Case 2 for method Foo1.

Types of Inheritance
1. Implementation Inheritance: A class is parent of another class and the methods implemented in parent class
can be used in the child class.
2. Interface Inheritance: An interface is parent of another interface or class. The Child only inherits the Prototype
of the method but will have to provide implementation to the method i.e. implementation is not inherited.
class CA : CA1, CA2 //Invalid
{}
.Net doesnt support Multiple Implementation Inheritance because if the same method is present in both the
parent classes and not overridden in the child class, it would be ambiguous to call the method on the variable of child
class.
For Example: If we have a method Foo in both CA1 and CA2 and if not overridden in the class CA then following
would be ambiguous.
CA a = new CA();
a.Foo() //Ambiguous call as both CA1 and CA2 qualify for execution.
class CA : IA1, IA2 //Valid - .Net supports Multiple Interface Inheritance.
{}

In this case we have to compulsory implement in the methods of Interface in the class and thus we dont have any
ambiguity.
interface IA : IA1, IA2
{
.
}
//An interface can inherit from more than one interface
//In this case the class implementing IA must implement members of both IA1 and IA2 also.
We can write a class which inherits from another class and also implements an interface.
In C#: class CA : CA1, IA1, IA2

Note: In sequence parent class should come before interface

Polymorphic: class CA1: IA and class CA2: IA One Interface implemented by two or more classes.
Not Polymorphic: class CA: IA1, IA2 One class implementing two interfaces.

Summary:
In this article of Interface, we have seen the use of interface in .net and how one can use interface inheritance that
can call methods without ambiguity.

S-ar putea să vă placă și