.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Interface in C#

Posted By:Abhisek Panda       Posted Date: October 27, 2009    Points: 25    Category: C#    URL: http://www.dotnetspark.com  
 

Interface


Interface is the collection of abstract method that are implemented in different applications. It is defined as the process of defining once and use it when and where we want to implement.

It defines derivation of new classes from more than one super class, that means by using interface we can implement multiple inheritance.

Interface can not be instantiated but can have reference variable. Interface can contain only final and static datamembers as well as methods of abstract type, it means the methods have to be overridden by child.

There is an interface means there must be a child class which has to override all the methods of interface. If it will not override completely the it should be abstract class and offers to form concrete class to override.



Syntax to declare interface


interface NAME
{
final static datamembers;
methods;
}


Syntax to declare a child


class CHILD:Interface Class
{
//defination of methods of interface class
}


See the following sample program with one interface and two child implementing the interface class.


interface Area
{
double Get(double x, double y);
}

class Circle:Area
{
//overriding interface method
public double Get(double x, double y)
{
return 3.14* x * x;
}
}

class Rect:Area
{
//overriding interface method
public double Get(double x, double y)
{
return x * y;
}
}

class InterfaceDemo
{
static void Main(string[] args)
{
//creating objects of child class
Rect r = new Rect();
Circle c = new Circle();
double k = r.Get(10.2, 12.9);
double l = c.Get(5.0,0);
Console.WriteLine("Area of rect= {0}", k);
Console.WriteLine("Area of circle= {0}", l);
}
}

OUTPUT

Area of rect=131.58
Area of circle=78.5 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend