What is an Interface?

Posted Tue, Sep 1 2009 9:29 by Deborah Kurata

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".)

Enjoy!

Filed under: , , , ,

Comments

# Basic Pillars of an Object-Oriented System

Tuesday, September 01, 2009 12:16 PM by Deborah's Developer MindScape

The four basic elements of an object-oriented system are abstraction , encapsulation , inheritance ,

# Interesting Finds: September 2, 2009

Wednesday, September 02, 2009 7:11 AM by Jason Haley

Interesting Finds: September 2, 2009

# re: What is an Interface?

Wednesday, January 13, 2010 7:20 AM by Juan

Thanks for the explanation.... Very clear

# re: What is an Interface?

Friday, February 04, 2011 3:17 PM by Mona

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

# re: What is an Interface?

Monday, December 05, 2011 9:52 PM by Ben

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

# re: What is an Interface?

Wednesday, April 11, 2012 1:54 PM by Mike Angelastro

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.

Mike

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: