Pages

Sunday, 28 June 2015

An Inherited Member As an Implementation

An Inherited Member As an Implementation

A class implementing an interface can inherit the code for an implementation from one of its base classes. For example:
 public interface IPrintIdentity 
{
 string getName();
 int getAge();
 void printIdentity();
}

public class date
{
 public int year, month, day;
 public date(int Year,int Month,int Day)
 {
  year = Year;
  month = Month;
  day = Day;
 }
}

public class person
{
 public string firstName;
 public string lastName;
 public date bornDate;
 public int accountBalance;
}

public class employee:person,IPrintIdentity  //employee inherit person class
{
 public string company;

 public string getName()
 {
  return firstName + " " + lastName;
 }

 public int getAge()
 {
  return DateTime.Now.Year - bornDate.year;
 }

 public void printIdentity()
 {
  Console.WriteLine("Person's Name:" + getName());
  Console.WriteLine("Age:" + getAge());
  Console.WriteLine("Work In:" + company);
 }
}

class Program
{ 
 static void Main(string[] args)
 {
  employee me = new employee { firstName = "Yang", lastName = "Sopiana", 
   bornDate = new date(1986, 11, 2),company="Samsung Electronics Indonesia"};
  me.printIdentity();
 }
}

Implementing Interface Members Virtually

An implicitly implemented interface member is, by default, sealed. It must be marked virtual or abstract in the base class in order to be overridden. For example:
public interface IPrintIdentity 
{
 string getName();
 int getAge();
 void printIdentity();
}

public class date
{
 public int year, month, day;
 public date(int Year,int Month,int Day)
 {
  year = Year;
  month = Month;
  day = Day;
 }
}

public class person : IPrintIdentity
{
 public string firstName;
 public string lastName;
 public date bornDate;
 public int accountBalance;

 public string getName()
 {
  return firstName + " " + lastName;
 }

 public int getAge()
 {
  return DateTime.Now.Year - bornDate.year;
 }

 public virtual void printIdentity()  //virtual implementation
 {
  Console.WriteLine("Person's Name:" + getName());
  Console.WriteLine("Age:" + getAge());
 }
}

public class employee:person
{
 public string company;

 public override void printIdentity() //override printIdentity method
 {
  Console.WriteLine("Person's Name:" + getName());
  Console.WriteLine("Age:" + getAge());
  Console.WriteLine("Work In:" + company);
 }
}

class Program
{ 
 static void Main(string[] args)
 {
  employee me = new employee { firstName = "Yang", lastName = "Sopiana", 
   bornDate = new date(1986, 11, 2),company="Samsung Electronics Indonesia"};
  me.printIdentity();
 }
}

Extending an Interface

You saw earlier that interface implementations can be inherited from base classes. But an interface itself can inherit from one or more other interfaces.
  • To specify that an interface inherits from other interfaces, place the names of the base interfaces in a comma-separated list after a colon following the interface name in the interface declaration
  • Unlike a class, which can have only a single class name in its base class list, an interface can have any number of interfaces in its base interface list.
For example:
public interface IUndoable 
{ 
 void Undo(); 
}

public interface IRedoable : IUndoable 
{ 
 void Redo(); 
}

0 comments:

Post a Comment