Interface
Interface used for controlling the class. Interface does not contain and implement anything but method which has created by the interface will be implemented by Class. Suppose there is a class University, Interface UGC on base and class College is a child as in the following code snippet:
- Interface UGC {
- Public void Get()
- Public void Display()
- }
- Class University: UGC
- String Name, Adress, Type;
- Public void Get() {
- Console.Write(“Enter the University Name, Adress and Type: ”);
- This.Name = Console.ReadLine();
- This.Adress = Console.ReadLine();
- This.Type = Console.ReadLine();
- }
- Public void Display() {
- Console.WriteLine(“University Name is: ”+this.Name);
- Console.WriteLine(“University Address is: ”+this.Adress);
- Console.WriteLine(“University Type is: ”+this.Type);
- }
- Class College: University
- String Name, Adress, Type;
- Public void Get() {
- Console.Write(“Enter the College Name, Adress and Type: ”);
- This.Name = Console.ReadLine();
- This.Adress = Console.ReadLine();
- This.Type = Console.ReadLine();
- }
- Public void Display() {
- Console.WriteLine(“College Name is: ”+this.Name);
- Console.WriteLine(“College Address is: ”+this.Adress);
- Console.WriteLine(“College Type is: ”+this.Type);
- }
- Class Interface_Implementation
- Public void Main(); {
- College obj = new College();
- Obj.get();
- Obj.Display();
- }
- Console.ReadLine();
- }
- }
In the above code snippet we can see UGC interface is not containing and implementing anything but the method which has created by UGC is implemented by both base and child class. Also we can tell Interface is a contract between classes about the method which was created by Interface and will be implemented by class.
Recap
Interface does not implement and contain anything but it is used for controlling the class.