Wednesday, September 9, 2009

Interface Example-2

interface INode
{
string Text{get;set;}
object Tag{get;set;}

int Height{get;set;}

int Width{get;set;}

float CalculateArea();
}
public class Node : INode
{
public Node(){}
#region INode Members
public string Text
{
get
{
return m_text;
}
set
{
m_text = value;
}
}
private string m_text;
public object Tag
{
get
{
return m_tag;
}
set
{
m_tag = value;
}
}
private object m_tag = null;
public int Height
{
get
{
return m_height;
}
set
{
m_height = value;
}
}
private int m_height = 0;
public int Width
{
get
{
return m_width;
}
set
{
m_width = value;
}
}
private int m_width = 0;
public float CalculateArea()
{
if ((m_width < 0) (m_height < 0))
return 0;
return m_height * m_width;
}
#endregion
}
class ClonableNode : INode, ICloneable
{
public object Clone()
{
return null;
}
#region INode Members
public string Text
{
get
{
return m_text;
}
set
{
m_text = value;
}
}
private string m_text;
public object Tag
{
get
{
return m_tag;
}
set
{
m_tag = value;
}
}
private object m_tag = null;
public int Height
{
get
{
return m_height;
}
set
{
m_height = value;
}
}
private int m_height = 0;
public int Width
{
get
{
return m_width;
}
set
{
m_width = value;
}
}
private int m_width = 0;
public float CalculateArea()
{
if ((m_width < 0) (m_height < 0))
return 0;
return m_height * m_width;
}
#endregion
}
/// <summary>
/// Summary description for TestClass.
/// </summary>
//class TestClass
//{
// /// <summary>
// /// The main entry point for the application.
// /// </summary>
// [STAThread]
// static void Main (string[] args)
// {
// INode node;
// node = new Node();
// node.Height = 10;
// node.Width = 20;
// float area = node.CalculateArea();
// Console.WriteLine(area);
// Console.WriteLine();
// // multiple inherirance
// ClonableNode nodeC = new ClonableNode();
// object obj = nodeC.Clone();
// nodeC.Height = 200;
// area = nodeC.CalculateArea();
// Console.WriteLine(area);
// if(nodeC is INode)
// Console.WriteLine("nodeC is object of INode type");
// else Console.WriteLine("nodeC isn't object of INode type");
// }
//}
static void Main (string[] args)
{
INode node;
node = new Node();
node.Height = 10;
node.Width = 20;
float area = node.CalculateArea();
Console.WriteLine(area);
Console.WriteLine();
Console.WriteLine("Interfaces (multiple inherirance)");
ClonableNode nodeC = new ClonableNode();
object obj = nodeC.Clone();
nodeC.Height = 200;
area = nodeC.CalculateArea();
Console.WriteLine(area);
if (nodeC is INode)
Console.WriteLine("nodeC is object of INode type");
else Console.WriteLine("nodeC isn't object of INode type");
}

____________________________________
Example
2 interface with same method name, how to implement in derived class
interface IA

    {

        void Display();
    }

interface IB
    {
        void Display();
    }

class CA
    {
        public void Display()
        {
            Console.WriteLine("I am from CA");
        }
    }

 class CB : CA, IA, IB
    {}   

class BB : CA, IA, IB
  {
  void IA.Display() { Console.WriteLine("I am from IA.Display"); }
  void IB.Display() { Console.WriteLine("I am from IB.Display"); }
  //public new void Display() { Console.WriteLine("I am from BB.Display"); }
 }

static void Main(string[] args)
        {
   
         
       
     CB cb = new CB();
     cb.Display(); // displays class CA Display method.

     BB bb = new BB();
     bb.Display(); // displays class CA Display method.
     (bb as IA).Display(); // displays IA.Display
     (bb as IB).Display(); // displays class A Display method.
 }

 Output
I am from CA
I am from CA
I am from IA.Display
I am from IB.Display

____________________________________
Example
 

   interface IParentInterface

    {

        void ParentInterfaceMethod();
    }

    interface IMyInterface : IParentInterface
    {
        void MethodToImplement();
    }

    class InterfaceImplementer : IMyInterface
    {
        public void MethodToImplement()
        {
            Console.WriteLine("MethodToImplement() called.");
        }

        public void ParentInterfaceMethod()
        {
            Console.WriteLine("ParentInterfaceMethod() called.");
        }
    }
static void Main(string[] args)
        {  
InterfaceImplementer iImp = new InterfaceImplementer();
            iImp.MethodToImplement();
            iImp.ParentInterfaceMethod(); 
}

 
Output

MethodToImplement() called.
ParentInterfaceMethod() called.

1 comment:

  1. http://www.csharp-station.com/Tutorial/CSharp/lesson13
    http://stackoverflow.com/questions/9416000/abstract-base-class-implementing-derived-class-interface-methods
    http://stackoverflow.com/questions/19910342/c-sharp-interface-method-declared-in-base-class-need-not-be-again-implemented-in

    ReplyDelete