Wednesday, 2 March 2016

C#.NET -- INTERFACE

                                    INTERFACE 

Interface is a mechanism to achieve full abstraction that is it contains only abstract methods without body.

Interface is a contract between itself and it's  derived classes . and it acts like a blueprint for creating a new objects.

Ex :  oops ( rules and regulations) are blueprint for java and c# etc., like that interface is a blueprint for its derived class to provide a guide to do one task.

Interface cannot implement any thing so we can used that format  for further development of same format of derived classes.

features of interface :


  • The interface contains only method declarations and can‘t contain method definitions (similar to the abstract methods).
  • The interface can‘t contain any data members but can contain ―automatic properties (Already you know that the automatic property doesn‘t contain definitions for ―get and ―set accessors).
  • Interface methods are by default ―public. We can‘t use another access modifier like private and protected etc. there no need external declaration  of public also.
  • The interface can‘t be instantiated. That means you can‘t create an object for the interface.
  • The interface can‘t contain constructors.
  • The class that inherits the interface is called as Implementation Class.
  •  The implementation class should implement the definitions for all the interface methods. If not, it would generate compile time errors.
  • One interface can be inherited by any no. of classes (Hierarchical inheritance).
  • One class can inherit any no. of interfaces (Multiple inheritance).
  •  The interface methods can‘t be declared as ―virtual or ―static in the interface definition.


Syntax :

interface definition:

interface interfacename 
{
  // empty methods declarations here
 }

implementation class definition  :

class classname : interfacename 
   public returntype methodname(arguments) 
  { 
    //some code 
  } 
 }



Sample Programs :

Write a Program on interface usage .



Output is :

the method is taken from parent interface


-----------------------------------------------------------------------------------------------------

Write a program to develop multiple inheritance using interface ?



Output is :
take this money childs!!
take this snakes childs !!
hello chidls 

So even there are same methods in two interface the program will execute .