Extends class and implements interface : Class Definition : Class C# Examples


C# Examples » Class » Class Definition »

 

Extends class and implements interface









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

interface  Talkable
{
      string  Table();
}

class  Animal  {  }

class  Cat  :  Animal,  Talkable
{
      string  Talkable.Table()  {  
              return  "miao";  
      }
}

class  Dog  :  Animal,  Talkable
{
      string  Talkable.Table(){  
            return  "bulk";  
      }
}

class  Elephant  :  Animal
{
        
}

class  MainClass
{
      static  void  Main()
      {
            Animal[]  AnimalArray  =  new  Animal[3];

            AnimalArray[0]  =  new  Cat();
            AnimalArray[1]  =  new  Elephant();
            AnimalArray[2]  =  new  Dog();

            foreach  (Animal  a  in  AnimalArray)
            {
                  Talkable  b  =  a  as  Talkable;
                  if  (b  !=  null)
                        Console.WriteLine("Baby  is  called:  {0}",  b.Table());
            }
      }
}
    
   
  
   



Output

Baby is called: miao
Baby is called: bulk


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» Class Definition