In the previous article What's really differs an Interface from a class?: Part I I tried to distinguish the interface concept from the class concept by studying and parsing each one from a pure nature point of view and I tried to put the focus on first concept as it doesn't already clear for some people. In this article, I will respond to the second question posed in the previous one.
Compare the Interface function to the class function:
Generally classes including abstract classes serve as base model to other classes those inherit them and extend them through overriding members. Especially, abstract classes are perfect tools to generate new derived types, at the other hand, interfaces serve as bridge between different types even they are heterogeneous and I mean that they don't belong to the same family each others. Although the class serves to create new derived types, interfaces provide more functions. To better understand the diver services provided by both classes and interfaces let take a look at this example. Imagine an abstract class Hominid that serves as a base model to derived classes or types. This is a hominid representation.
class Homini
{
public abstract string Category{get; set;}
public abstract uint Age{get; set;}
public abstract uint Height{get; set;}
public abstract uint Weight{get; set;}
public abstract void Behaviour ();
}
Given that those derived classes are Human, Chimpanzee and orangutan. We can implement them as follow
The human class:
|
public class Human:Hominid { public Human() : base() { } private string _Category; public override string Category { get { return _Category; } set { _Category = value; } } private uint _Age; public override uint Age { get { return _Age; } set { _Age = value; } } private uint _Height; public override uint Height { get { return _Height; } set { _Height = value; } } private uint _Weight; public override uint Weight { get { return _Weight; } set { _Weight = value; } } public override void Behaviour() { Console.WriteLine("I'm a human"); Console.ReadKey(true); Console.Beep(); } } |
The Chimpanzee class:
|
public class Chimpanzee:Hominid { public Chimpanzee() : base() { } private string _Category; public override string Category { get { return _Category; } set { _Category = value; } } private uint _Age; public override uint Age { get { return _Age; } set { _Age = value; } } private uint _Height; public override uint Height { get { return _Height; } set { _Height = value; } } private uint _Weight; public override uint Weight { get { return _Weight; } set { _Weight = value; } } public override void Behaviour() { Console.WriteLine("I'm a Chimpanzee"); Console.ReadKey(true); Console.Beep(); } } |







Sarika JagtapPosted Jul 26, 2010, 7:10 AM
Really nice article! concepts are more clear now!