Click here to Skip to main content
Licence CPOL
First Posted 26 Oct 2007
Views 15,069
Downloads 149
Bookmarked 10 times

Partial Class in Polymorphism

By | 26 Oct 2007 | Article
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)

About the Author

RajeshKumar Shan

Web Developer

India India

Member

IT Professional

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 26 Oct 2007
Article Copyright 2007 by RajeshKumar Shan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid