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

Leave a Comment

(required) 
(required) 
(optional)
(required)