Interface Implementation

Interface members are defined like class members except for a few important differences:

  • No access modifiers (public, private, protected, or internal) are allowed- all interface members are implicitly public.
  • Interface members can’t contain code bodies
  • Interface can’t define field members
  • It’s members can’t be defined using the keywords static, virtual, abstract, or sealed.
  • Type definition members are forbidden

To hide base interfaces member just put new keyword before derived interfaces member.

interface BaseInterface
{
         void MyFunction();

}
interface DerievdInterfaces :  BaseInterface

{
           new void MyFunction();
}

Implementing Interfaces in Classes

Implicit interface implementation

// Program: Example of Implicit interface
// By: H.M. Saiful Islam

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

namespace OOP
{

    public interface MyInterface
    {
        void MyMethod();
        void Second();
    }

    // Base Class
    public class BaseClass : MyInterface
    {
        public virtual void MyMethod()
        {
            Console.WriteLine("It's Base class's method");
        }
        public virtual void Second()
        {
            Console.WriteLine("It's second method");
        }
    }

    // Derived class
    public class DerivedClass : BaseClass
    {
        public override void MyMethod()
        {
            Console.WriteLine("Implicit interface's example");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            BaseClass MBC;
            DerivedClass MDB = new DerivedClass();
            MBC = MDB;
            MBC.MyMethod();
            Console.ReadKey();
        }
    }
}

Explicit Interface implementation

// Program: Example of Explicit Interface
// By: H.M. Saiful Islam

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

namespace OOP
{

    public interface MyInterface
    {
        void MyMethod();
        void Second();
    }

    public class MyClass : MyInterface
    {
        void MyInterface.MyMethod()
        {
            Console.WriteLine("Explicit Interface");
        }

        public void Second()
        {
            Console.WriteLine("Another example !");
        }
    }
   class Program
    {
        static void Main(string[] args)
        {
            MyClass MyObj = new MyClass();
            MyInterface MyInt = MyObj;
            MyInt.MyMethod();
            Console.ReadKey();
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s