IT Community - Software Programming, Web Development and Technical Support

What is the use of interface?

This is a discussion on What is the use of interface? within the C# Programming forums, part of the Software Development category; hi all, While implementing an interface, we have to define all the methods declared int the interface. Like Class1 : interface1 { } ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-08-2008, 04:22 AM
Mramesh Mramesh is offline
D-Web Sr.Programmer
 
Join Date: Sep 2007
Location: Chennai
Posts: 106
Mramesh is on a distinguished road
Send a message via MSN to Mramesh
Question What is the use of interface?

hi all,

While implementing an interface, we have to define all the methods declared int the interface. Like
Class1 : interface1
{
}
Class2 : interface1
{
}

Here we have to write definition of all the methods declared in interface1 in both classes class1 and class2 then what is the use of interface? Why we can't write definition and declaration directly in both class?
__________________
M.Ramesh Kumar
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-08-2008, 04:26 AM
a.deeban a.deeban is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 279
a.deeban is on a distinguished road
Post Re: What is the use of interface?

.Net will not support multiple Inheritance(Inheriting from two parent class) as C++ do. So inorder to achieve multiple inheritance we are using inteface. A class can inherit only one class but it can implement more than one interface

Suppose there are three classes class1, Class2 and Class3
and two Interfaces Interface1 and Interface2
In C# we cannot write like

Class1: Class2, Class3
{

}

as c# will not support multiple inheritance

but we can write like this

Class1: Class2, Inreface1
{

}
Here we are inheriting Class2 and implementing Interface1 there by achieving multiple inheritance
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-08-2008, 04:28 AM
Sundaram Sundaram is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Location: chennai
Posts: 117
Sundaram is on a distinguished road
Send a message via MSN to Sundaram Send a message via Yahoo to Sundaram
Post Re: What is the use of interface?

Defining the ImemberProfile Interface

Defining an interface is a lot like defining a class, except that you don't have to get involved in the details about how anything will work. It is a lot like the header files in C/C++. For example:

Code:
public interface IMemberProfile
{
  string FirstName {get;set;}
  string LastName {get;set;}
  string Email {get;set;}
  System.Guid MemberID {get;set;}
}
This example defines an interface that will require that any class implementing it must provide read/write properties for FirstName, LastName, Email, and MemberID.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-08-2008, 04:31 AM
SaravananJ SaravananJ is offline
D-Web Programmer
 
Join Date: Aug 2007
Posts: 79
SaravananJ is on a distinguished road
Post Re: What is the use of interface?

Implementing the IMemberProfile Interface

To implement an interface, you start by marking that the class you are defining will implement the interface. In C#, this is similar to the syntax for inheritance.

Code:
public class MemberProfile: System.Web.UI.Page, IMemberProfile
In VB.NET, the syntax is slightly different.

Code:
Public Class MemberProfile
  Inherits System.Web.UI.Page
  Implements IMemberProfile

The compiler will handle warning you about anything that the interface requires that is not included in your class. For this example, you simply need to include properties for FirstName, LastName, Email, and MemberID.


Code:
public string FirstName
{
 get{return txtFirstName.Text ;}
 set {txtFirstName.Text = value;}
}

public string LastName
{
 get{return txtLastName.Text ;}
 set {txtLastName.Text = value;}
}

public string Email
{
 get{return txtEmail.Text ;}
 set {txtEmail.Text = value;}
}

public System.Guid MemberID
{
  get
  {
    if (m_uniqueID.CompareTo (null) == 0)
    {
       m_uniqueID = System.Guid.NewGuid();
    }
    return m_uniqueID;
  }
  set 
  {
    m_uniqueID = value;
  }
}
Implementing an interface really requires nothing more than the rules of encapsulation would recommend.
__________________
J.Saravanan
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-08-2008, 04:35 AM
Sathish Kumar Sathish Kumar is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 304
Sathish Kumar is on a distinguished road
Default Re: What is the use of interface?

Using Your Interface

Now that we have the interface defined and a UI that implements the interface, we are ready to write some business logic that will use this interface. Here we define two methods that will be used to retrieve the data to populate the UI, as well as a method to retrieve the data currently populated in the UI. In a real-world example, these methods would use a database to store the details. To keep this example simple, we will simply use local variables. To truly appreciate what is happening in this example, I recommend stepping through the code with the debugger and watching the properties being accessed.

The method to retrieve the current member profile details would be similar to this:

Code:
public static void RetrieveMemberProfile (IMemberProfile Profile)
{
 Profile.FirstName = "John";
 Profile.LastName = "Doe";
 Profile.Email = "jdoe@email.com";
 Profile.MemberID = 
    new System.Guid  
    ("{65C38236-CA96-4FF1-9142-00873B8BD333}");
}
The method to update the member profile details would be similar to this:

Code:
public static void UpdateMemberProfile (IMemberProfile Profile)
{
  string _FirstName;
  string _LastName;
  string _Email;
  string _MemberID;
  
  _FirstName = Profile.FirstName;
  _LastName = Profile.LastName;
  _Email = Profile.Email;
  _MemberID = Profile.MemberID.ToString();
}
In the Page_Load event, we will call the RetrieveMemberProfile method like this:

Code:
private void Page_Load(object sender, System.EventArgs e)
{
   // Put user code to initialize the page here
   if (!Page.IsPostBack)
   {
      BusinessRules.RetrieveMemberProfile (this);
   }
}
The click event handler for the button will look similar to this:

Code:
private void btnUpdate_Click(object sender, System.EventArgs e)
{
  BusinessRules.UpdateMemberProfile (this);
}
__________________
Sathish Kumar.R
Knowledge is meant to SHARE
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-08-2008, 04:51 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Smile Re: What is the use of interface?

Hi,

Here i am explaining the interface with an example.. I hope that, its very helpful one...

In the example below we have a class (Test) that implements 2 interfaces (I1 and I2). So the logic says that we should have implementations in the class Test for both MyFunction() methods from I1 and I2. BUT! As you see in the example below everything works fine with only 1 implementation. This is not as correct as we expect, because we can't be sure which of the two interfaces is implemented in the MyFunction method in class Test. So the solution is in another example (EXAMPLE2). In Example2 MyFunction() is implemented in class Test for each interface, by using a different method. (I1.MyFunction() -> for I1, I2.MyFunction() -> for I2).

EXAMPLE 1
Code:
using System;

namespace Interfaces
{
	interface I1
	{
		void MyFunction();
	}
	interface I2
	{
		void MyFunction();
	}
	class Test : I1,I2
	{
		public void MyFunction()
		{
			Console.WriteLine("Guess which interface I represent???!");
		}
	}
	class AppClass
	{
		public static void Main(string[] args)
		{
			Test t=new Test();
			I1 i1=(I1)t;
			i1.MyFunction();
			I2 i2=(I2)t;
			i2.MyFunction();
		}
	}
}
EXAMPLE 2
Code:
using System;

namespace Interfaces
{
	interface I1
	{
		void MyFunction();
	}
	interface I2
	{
		void MyFunction();
	}
	class Test : I1,I2
	{
		void I1.MyFunction()
		{
			Console.WriteLine("Now I can say this here is I1 implemented!");
		}
		void I2.MyFunction()
		{
			Console.WriteLine("Now I can say this here is I2 implemented!");
		}
	}
	class AppClass
	{
		public static void Main(string[] args)
		{
			Test t=new Test();
			I1 i1=(I1)t;
			i1.MyFunction();
			I2 i2=(I2)t;
			i2.MyFunction();
		}
	}
}
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the Servlet Interface? Arun Java Programming 2 09-10-2007 04:35 AM
What is the Set interface? anbuchezhians Java Programming 4 09-10-2007 04:18 AM
Map interface in Collection Api leoraja8 Java Programming 6 09-06-2007 03:51 AM
c# interface Sathish Kumar C# Programming 2 09-02-2007 11:32 PM
Do we have interface to schedule a task like ITaskscheduler interface for desktop.... theone Mobile Software Development 1 07-24-2007 11:29 PM


All times are GMT -7. The time now is 07:52 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0