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 { } ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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 |
| Sponsored Links |
| |||
| .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 |
| |||
| 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;}
} |
| |||
| 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 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;
}
}
__________________ J.Saravanan |
| |||
| 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}");
} 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();
} Code: private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!Page.IsPostBack)
{
BusinessRules.RetrieveMemberProfile (this);
}
} Code: private void btnUpdate_Click(object sender, System.EventArgs e)
{
BusinessRules.UpdateMemberProfile (this);
}
__________________ Sathish Kumar.R ![]() Knowledge is meant to SHARE |
| |||
| 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();
}
}
} 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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |