Wednesday, March 14, 2012

Polymorphism via Interfaces - II

In previous post we discussed polymorphism via interface. In this post we will expand it to demonstrate how you can implement more than one interface in a class and how you can pass an object that implements the interface to another method and have it do something.

In the example below, we declared two interfaces - IPrintable and IUserType and then created three classes that will implement these two interfaces. We then declared another static class with a static method that can accept the object and do something with it.

IPrintable Interface
Paste your text here.using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interfaces
{
    interface IPrintable
    {
        void print();
    }
}


IUserType Interface

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interfaces
{
    interface iUserType
    {
        void userType();
    }
}


First Class - Implementing both Interfaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interfaces
{
    class Employee:IPrintable,iUserType
    {
        public string Name { get; set; }
        public bool IsFullTime { get; set; }
        
        public void print()
        {
            Console.WriteLine("Name:={0},FullTime={1}", Name, IsFullTime);
        }
        public void userType()
        {
            Console.WriteLine("Employee");
        }
    }
}


Second Class - Implementing both Interfaces
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interfaces
{
    class Instructor :IPrintable,iUserType
    {
        public string Name { get; set; }
        public string SeniorityLevel { get; set; }
        public bool IsTenured { get; set; }

        public void print()
        {
            Console.WriteLine("Name:={0},SeniorityLevel={1},Tenured={2}", 
                              Name, SeniorityLevel, IsTenured);
        }
        public void userType()
        {
            Console.WriteLine("Instructor");
        }
    }
}



Third Class - Implementing both Intefaces
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interfaces
{
    class Student : IPrintable,iUserType
    {
        public string Name { get; set; }
        public string StudentID { get; set; }
        public String GradeLevel { get; set; }

        public void print()
        {
            Console.WriteLine("Name:={0},GradeLevel={1},StudentID={2}", 
                               Name, GradeLevel, StudentID);
        }
        public void userType()
        {
            Console.WriteLine("Student");
        }
    }
}



Static Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interfaces
{
    static class Print
    {
        public static void Printout(IPrintable printableObject)
        {
            printableObject.print();
        }
        public static void UserType(iUserType UserType)
        {
            UserType.userType();
        }
    }
}





Calling Method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interfaces
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee emp = new Employee();
            emp.Name = "1st Employee";
            emp.IsFullTime = true;

            Print.Printout(emp);
            Print.UserType(emp);
            Console.WriteLine(Environment.NewLine);

            Instructor ins = new Instructor();
            ins.Name = "1st Instructor";
            ins.IsTenured = true;

            Print.Printout(ins);
            Print.UserType(ins);
            Console.WriteLine(Environment.NewLine);

            Student std = new Student();
            std.Name = "1st Student";
            std.StudentID = "123456";
            std.GradeLevel = "Junior";

            Print.Printout(std);
            Print.UserType(std);
            Console.Read();

        }
    }
}




In the example above, three classes implement IPrintable and IUserType interface and the Main method calls static methods Printout and UserType declared in Print static class, passing the object.

Notice the Printout and UserType static methods call Print and userType that are implemented in the passed objects.

The only reason you can be sure that regardless of the object type you pass to the static Print and UserType method, the code will work just fine is because any class that implements an interface is guaranteed to implement all the signatures that are declared in the interface. An interface is a contract and any class implementing an interface must implement all its methods.

No comments:

Post a Comment