C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Interface in C#


Posted Date: 03 Oct 2009    Resource Type: Articles    Category: .NET Framework
Author: Abhisek PandaMember Level: Gold    
Rating: 1 out of 5Points: 10



What is an 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.

SYNTAXES

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



Responses

Author: Deepika Haridas    08 Oct 2009Member Level: Diamond   Points : 1
Well formatted post..

Good. Keep it up!!

--
Thanks & Regards,
Deepika
Editor


Author: Abhisek Panda    08 Oct 2009Member Level: Gold   Points : 0
Thanks Deepika for your support and continuous encouragement.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Interface  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Create Your Own Exceptions in C#
Previous Resource: Abstract Method and Abstract Class
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use