Pages

Sunday, 28 June 2015

Declaring and Implementing an Interface

Declaring an Interface

The important things to know about declaring an interface are the following:
  • An interface declaration cannot contain data members and static members
  • An interface declaration can contain only declarations of the following kinds of nonstatic function members, such: Methods, Properties, Events and Indexers
  • The declarations of these function members cannot contain any implementation code.
  • By convention, interface names begin with an uppercase I and Pascal Casing(e.g., IComparable, IFormattable, IFileCompression).
  • Like classes and structs, interface declarations can also be split into partial interface declarations.
This is syntax declaring an interface:
interface [Interface Name]
{
 [Methods declaration]
}

There is an important difference between the accessibility of an interface and the accessibility of interface members:
  • An interface declaration can have any of the access modifiers public, protected, internal, or private.
  • Members of an interface are implicitly public, and no access modifiers.
See following example:
public interface IPrintIdentity 
{
 string getName();
 string getAnniversary();
 void printIdentity();
 //private void printIdentity2();  //error private modifier is not allowed
} 

Implementing an Interface

Only classes or structs can implement an interface. As shown in the Sort example, to implement an interface, a class or struct must
  • Include the name of the interface in its base class list
  • Supply implementations for each of the interface’s members
Some important things to know about implementing interfaces are the following:
  • If a class implements an interface, it must implement all the members of that interface.
  • If a class is derived from a base class and also implements interfaces, the name of the base class must be listed in the base class list before any interfaces.
See following code sample
public interface IPrintIdentity   //interface declaration
{
 string getName();
 string getAnniversary();
 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  //interface implementation
{
 public string firstName;
 public string lastName;
 public date bornDate;
 
 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());
 }
}

public class company : IPrintIdentity //interface implementation
{
 public string name;
 public date builtDate;

 public string getName()
 {
  return name;
 }

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

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

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

The code will produce following display:
Person's Name:Yang Sopiana
Age:29
Company's Name:Samsung Electronics Indonesia
Age:25

0 comments:

Post a Comment