dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersShine S
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » .NET programming » ASP.NET/Web Applications

Interface Fundamental in .Net


Posted Date:     Category: ASP.NET/Web Applications    
Author: Member Level: Gold    Points: 8


This article is giving information of interface in .Net.



 


An interface is similar to a class, but it provides a specification rather than an implementation for its members. An interface is special in the following ways:

A class can implement multiple interfaces. In contrast, a class can inherit from only a single class.

Interface members are all implicitly abstract. In contrast, a class can provide both abstract members and concrete members with implementations.

Structs can implement interfaces. In contrast, a struct cannot inherit from a class.

An interface declaration is like a class declaration, but it provides no implementation for its members, as all its members are implicitly abstract. These members will be implemented by the classes and structs that implement the interface. An interface can contain only methods, properties, events, and indexers, which noncoincidentally are precisely the members of a class that can be abstract.

Here is a slightly simplified version of the IEnumerator interface, defined in System.Collections:

public interface IEnumerator
{
bool MoveNext();
object Current {get;}
}


Interface members have the same accessibility as the interface type, and they cannot declare an access modifier.

Implementing an interface means providing a public implementation for all its members:

internal class Countdown : IEnumerator
{
int count = 11;
public bool MoveNext() { return count-- > 0 ; }
public object Current { get { return count; } }
}


You can implicitly cast an object to any interface that it implements. For example:

IEnumerator e = new Countdown();
while (e.MoveNext())
Console.Write (e.Current); // 109876543210


Extending an Interface



Interfaces may derive from other interfaces. For instance:

public interface IUndoable { void Undo(); }
public interface IRedoable : IUndoable { void Redo(); }


IRedoable inherits all the members of IUndoable.


Explicit Interface Implementation


Implementing multiple interfaces can sometimes result in a collision between member signatures. You can resolve such collisions by explicitly implementing an interface member. Consider the following example:

interface I1 { void Foo(); }
interface I2 { int Foo(); }

public class Widget : I1, I2
{
public void Foo()
{
Console.WriteLine
("Widget's implementation of I1.Foo");
}

int I2.Foo()
{
Console.WriteLine
("Widget's implementation of I2.Foo");
return 42;
}
}


Because both I1 and I2 have conflicting Foo signatures, Widget explicitly implements I2's Foo method. This lets the two methods coexist in one class. The only way to call an explicitly implemented member is to cast to its interface:

Widget w = new Widget();
w.Foo(); // Widget's implementation of I1.Foo
((I1)w).Foo(); // Widget's implementation of I1.Foo
((I2)w).Foo(); // Widget's implementation of I2.Foo


Another reason to explicitly implement interface members is to hide members that are highly specialized and distracting to a type's normal use case. For example, a type that implements ISerializable would typically want to avoid flaunting its ISerializable members unless explicitly cast to that interface.

Implementing Interface Members Virtually


An implicitly implemented interface member is, by default, sealed. It must be marked virtual or abstract in the base class to be overridden. For example:

public interface IUndoable { void Undo(); }

public class TextBox : IUndoable
{
public virtual void Undo()
{
Console.WriteLine ("TextBox.Undo");
}
}

public class RichTextBox : TextBox
{
public override void Undo()
{
Console.WriteLine ("RichTextBox.Undo");
}
}


Calling the interface member through either the base class or the interface calls the subclass's implementation:

RichTextBox r = new RichTextBox();
r.Undo(); // RichTextBox.Undo
((IUndoable)r).Undo(); // RichTextBox.Undo
((TextBox)r).Undo(); // RichTextBox.Undo


An explicitly implemented interface member cannot be marked virtual, nor can it be overridden in the usual manner. It can, however, be reimplemented.

Reimplementing an Interface in a Subclass



A subclass can reimplement any interface member already implemented by a base class. Reimplementation hijacks a member implementation (when called through the interface) and works whether or not the member is virtual in the base class. It also works whether a member is implemented implicitly or explicitly—although it works best in the latter case, as we will demonstrate.

In the following example, TextBox explicitly implements IUndo.Undo, so it cannot be marked as virtual. To "override" it, RichTextBox must reimplement IUndo's Undo method:

public interface IUndoable { void Undo(); }

public class TextBox : IUndoable
{
void IUndoable.Undo()
{ Console.WriteLine ("TextBox.Undo"); }
}

public class RichTextBox : TextBox, IUndoable
{
public new void Undo()
{ Console.WriteLine ("RichTextBox.Undo"); }
}


Calling the reimplemented member through the interface calls the subclass's implementation:

RichTextBox r = new RichTextBox();
r.Undo(); // RichTextBox.Undo Case 1
((IUndoable)r).Undo(); // RichTextBox.Undo Case 2


Assuming the same RichTextBox definition, suppose now that TextBox implemented Undoimplicitly:

public class TextBox : IUndoable
{
public void Undo()
{ Console.WriteLine ("TextBox.Undo"); }
}


This would give us another way to call Undo, which would "break" the system, as shown in Case 3:

RichTextBox r = new RichTextBox();
r.Undo(); // RichTextBox.Undo Case 1
((IUndoable)r).Undo(); // RichTextBox.Undo Case 2
((TextBox)r).Undo(); // TextBox.Undo Case 3


Case 3 demonstrates that reimplementation hijacking is effective only when a member is called through the interface, and not through the base class. This is usually undesirable as it can mean inconsistent semantics, making reimplementation most appropriate for overriding explicitly implemented interface members.





Did you like this resource? Share it with your friends and show your love!


Responses to "Interface Fundamental in .Net"
Author: macxima    20 Oct 2010Member Level: Gold   Points : 0
Really! good explaination.


Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Namespace in C#
    Previous Resource: AdRotator control with SQLServer
    Return to Resources
    Post New Resource
    Category: ASP.NET/Web Applications


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Interface Example  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.