Declaring an Interface
The important things to know about declaring an interface are the following:
There is an important difference between the accessibility of an interface and the accessibility of interface members:
- 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.
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
, orprivate
. - Members of an interface are implicitly
public
, and no access modifiers.
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
The code will produce following display:
- Include the name of the interface in its base class list
- Supply implementations for each of the interface’s members
- 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.
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
Age:29
Company's Name:Samsung Electronics Indonesia
Age:25
0 comments:
Post a Comment