March 15, 2007
Class provides a definition to override the interface’s abstract definitions. This is one of the ways interface is defined.
But in the following example the program Employee class doesn’t provide a definition to override the interface’s abstract definitions. The situation is same in the Animal abstract class as well. That means there is no keyword “override” when defining Work() method within the Employee class and within the Animal abstract class. Anybody explain please.
using
System;
public
interface IWorking
{
string Work();
}
class
Employee : IWorking
{
private string name;
public Employee(string name)
{
this.name = name;
}
public string GetName()
{
return name;
}
public string Work()
{
return "I do my job";
}
}
abstract
class Animal : IWorking
{
protected string name;
public Animal(string name)
{
this.name = name;
}
public string GetName()
{
return name;
}
public abstract string Work();
}
class
Dog : Animal
{
public Dog(string name): base(name)
{
}
public override string Work()
{
return "I watch the house";
}
}
class
Cat : Animal
{
public Cat(string name) : base(name)
{
}
public override string Work()
{
return "I catch mice";
}
}
class
DemoWorking
{
public static void Main()
{
Employee bob = new Employee("Bob");
Dog spot = new Dog("Spot");
Cat puff = new Cat("Puff");
Console.WriteLine("{0} says {1}", bob.GetName(), bob.Work());
Console.WriteLine("{0} says {1}", spot.GetName(), spot.Work());
Console.WriteLine("{0} says {1}", puff.GetName(), puff.Work());
}
}
/*
Output:
Bob says I do my job
Spot says I watch the house
Puff says I catch mice
*/