Click here to Skip to main content
11,479,162 members (64,233 online)
Click here to Skip to main content

Partial Class in Polymorphism

, 26 Oct 2007 CPOL 19.3K 287 11
Rate this:
Please Sign up or sign in to vote.
Partial class implementation using C# in polymorphism.

Screenshot - PartPoly.jpg

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.

// Partial types in abstract class.
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.

// Partial types in Interfaces.
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.

// Partial types in virtual methods.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

RajeshKumar Shanmugam
Web Developer
India India
IT Professional

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 26 Oct 2007
Article Copyright 2007 by RajeshKumar Shanmugam
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid