Deborah's Developer MindScape

         Tips and Techniques for Web and .NET developers.

September 1, 2009

What is an Interface?

Filed under: C#,OOP,VB.NET @ 11:29 am

When talking about OO, the term “interface” has nothing to do with your
user interface. An interface defines a list of properties and methods that
a class can implement. But if the class implements a particular interface, it
must implement all properties and methods defined by that interface.

[To begin with an overview of OO, start here.]

For example, you can think of a phone as implementing a dial interface,
IDial. (By convention, interfaces begin with the letter I.) This interface defines Connect and MakeTone methods but does not implement them. This allows each phone to define how it connects and makes tones. Every phone that implements the IDial interface must provide an implementation
for both the connect and make tone methods.

An interface is different from a base class in that an interface defines
the list of properties and methods with no implementation. The implementation
is left to the class that implements the interface. A base class contains both the list of properties and methods and their implementation. The class that inherits from the base class does not need to provide any implementation.

In your application, you can implement interfaces provided in the
.NET Framework or define and implement your own interfaces. For
example, if you want to ensure that every MDI child form in your application
provides a standard set of methods, you could define an IMDIChild interface as follows:

In C#:

public interface IMDIChild
    bool ProcessDelete();
    bool ProcessNew();
    bool ProcessSave();

In VB:

Public Interface IMDIChild
    Function ProcessDelete() As Boolean
    Function ProcessNew() As Boolean
    Function ProcessSave() As Boolean
End Interface

In this example, the interface is comprised of three methods: ProcessDelete, ProcessNew, and ProcessSave. Each of these methods must be implemented in any class that implements this interface.

To implement an interface in a class, specify the interface when defining the class. For example, if the TimeSheetWin class implements the IMDIChild
interface, it would look like this:

In C#:

public class TimeSheetWin : IMDIChild

In VB:

Public Class TimeSheetWin
    Implements IMDIChild

Each class that implements this interface must provide an implementation
for all three of the interface methods.

[For an example of implementing the .NET IDataErrorInfo and INotifyPropertyChanged interfaces, see this link.]

Implementing an interface allows a class to be more formal about the
behavior it promises to provide. Interfaces form a contract between the
class and the rest of the application, and the compiler enforces this contract.
If your class claims to implement an interface, all methods defined by that
interface must be implemented before the class successfully compiles.

(Based on an except from "Doing Objects in Visual Basic 2005".)



  1.   Juan — January 13, 2010 @ 7:20 am    Reply

    Thanks for the explanation…. Very clear

  2.   Mona — February 4, 2011 @ 3:17 pm    Reply

    Thank you Deborah .I just learned what is an interface.It was very clear .

  3.   Ben — December 5, 2011 @ 9:52 pm    Reply

    Thanks a lot Deborah.Your explanation made every thing clear.

  4.   Mike Angelastro — April 11, 2012 @ 1:54 pm    Reply

    Hi Deborah,

    I have wondered why interfaces are needed. It seems like extra work for little gain if any. You can define an interface and implement it in a class as a reminder of all the properties and methods you need to include (VB10) because a space is added for each in a way that ensures you can’t use the class until you add the details for all the members. But once I do this I can comment out the implementation reference to that interface without effect to the class. If someone later adds to the interface, my class is not broken as a result.


RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2022 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs