Introduction
Partial classes are one of the coolest type available in .NET. It allows us to decompose the definitions and implementations into multiple classes.
This article help you to understand how to work with polymorphism by using partial types.
Background
Let us assume we know polymorphism. This article illustrates the differences between ordinary polymorphism and partial type polymorphism, and combining the polymorphism and partial types.
Partial Class Implementation
The sample code below implements the partial types in an abstract class. The AbstractClass
class has two partial type declarations with the same class name. In the first
AbstractClass
implementation, it has two methods called GetMessage
and GetClassID
. In the second AbstractClass
implementation,
it has one method called GetClassName
. MainAbstractClass
inherits the AbstractClass
base class. We have two partial classes but
all the methods are invoked in a single inheritance.
namespace PartialClassPoly
{
public abstract partial class AbstractClass
{
public abstract string GetMessage();
public abstract int GetClassID();
}
public abstract partial class AbstractClass
{
public abstract string GetClassName();
}
public class MainAbstractClass : AbstractClass
{
public override string GetMessage()
{
return "Message from AbstractClass";
}
public override int GetClassID()
{
return 1;
}
public override string GetClassName()
{
return "AbstractClass";
}
}
}
The below sample code implements the partial types in Interfaces. The pInterface
Interface has two partial type declarations with the same interface name. In the first
pInterface
implementation, it has a method called GetMessage
. In the second pInterface
implementation, it has two methods called
GetClassID
and GetClassName
. MainPolyInterfaces
implements the pInterface
Interface. We have two partial interfaces
but all the methods are invoked in a single Interface implementation.
namespace PartialClassPoly
{
public partial interface pInterface
{
string GetMessage();
}
public partial interface pInterface
{
int GetClassID();
string GetClassName();
}
public class MainPolyInterfaces : pInterface
{
public string GetMessage()
{
return "Message from PolyInterfaces";
}
public int GetClassID()
{
return 2;
}
public string GetClassName()
{
return "PolyInterfaces";
}
}
}
The below sample code implements partial types in virtual methods. The VirtualMethods
class has two partial type declarations with the same class name. In the first
VirtualMethods
implementation, it has two methods called GetMessage
and GetClassID
, In the second VirtualMethods
implementation,
it has a method called GetClassName
. MainVirtualMethods
inherits the VirtualMethods
base class. We have two partial classes
but all the methods are invoked in a single inheritance.
namespace PartialClassPoly
{
public partial class VirtualMethods
{
public virtual string GetMessage()
{
return "Message from Virtual Methods";
}
public virtual int GetClassID()
{
return 3;
}
}
public partial class VirtualMethods
{
public virtual string GetClassName()
{
return "VirtualMethods";
}
}
public class MainVirtualMethods : VirtualMethods
{
public override string GetMessage()
{
return "Message from MainVirtualMethods";
}
public override int GetClassID()
{
return 4;
}
public override string GetClassName()
{
return "MainVirtualMethods";
}
}
}
Points of Interest
Implementing partial types in polymorphism sets up the boundary to object oriented programming. The above partial type implementation represented using the overriding
concept in polymorphism and the same way of implementation can be done using the overloading concept in polymorphism. In short, partial types can decompose a class into
classes with the same name.
History
- Created on October 26 2007.