Simple Example of Inheritance and Interface (why do we use Inheritance and interface) in c# .Net

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
Advertisements

One thought on “Simple Example of Inheritance and Interface (why do we use Inheritance and interface) in c# .Net

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