Shezi
Shezi

Reputation: 1342

Understanding Interfaces in OOP

I am trying to learn OOP concept at an advance level. I was reading through the topic interfaces and have a confusion. But first, let me show you what exactly caused this confusion.
I tested this Code Sample. but I am confused with the use of interfaces. After implementing that code, it seems to me that I can call the method DoFirst from class A by simply creating an instance of it. so why use an interface at first place?

Something like this:

A myA = new A();
myA.DoFirst();

and similiarly,

B myB = new B();
myB.DoFirst();

In both the classes, i have to implement a method called 'DoFirst', so what good does interface provided to me?

Can't I just write these methods in different classes myself?

my Second question, say I have an interface that has 5 methods. If a class implements it, and only wants to provide implementation of 3 methods instead of writing code of all 5 methods supplied by the interface. Isn't this useless? why have access methods that i don't want?

can somebody answer these with example (Highly appreciated) please?

Upvotes: 0

Views: 559

Answers (3)

Nevyn
Nevyn

Reputation: 2683

Some Real Life examples - also, another way/reason to use interfaces.

In my own code I have an Engine which processes code to produce reports in Excel. In this engine, i had to write the code two different ways, one using the Microsoft Excel Interop, the other using the Open Office Interop. Rather than duplicate my entire engine to work two different ways, or write a lot of if statements in all the actual interop functions, I implemented an interface. Then I declared two classes, each one implementing the interface, but one uses Excel and the other uses open office. Then, in my code, I simple reference the interface and its functions and use a single if statement at the very beginning of the function to tell the interface which class to implement.

public class ExcelEngineInterop : ISSinterface
{ ... }

public class OOEngineInterop : ISSinterface
{ ... }

//cant use two variables with the same name, so use 1 interface reference instead
ISSinterface ssInt;
if(ExcelFlag)
    ssInt = new ExcelEngineInterop();
else
    ssInt = new OOEngineInterop();

//two VERY different functions between Excel and OpenOffice.
ssInt.OpenApp();
ssInt.OpenFile(fileName);

//etc etc and so on

This is the other reason to use an interface. When you need one block of code to act two (or more) different ways depending on some external flag.

Another Example.

There is a top level form with lots of custom user controls under it. The user fires a form level event, like a button click, but depending on which user controls are active and what settings are on them at the time the click happens, the controls themselves need to do something different. Rather than writing what could be a rediculously large number of if statements to make sure each one acts correctly from the top level, just implement an interface on each control, then do something like this:

public ButtonClick(object sender, EventArgs e)
{
    //note: I dont know which of my classes currentrightcontrol belongs to at the moment.
    //      it could be any one of 22 different controls. It must be cast to something
    //      in order to call the ButtonClick method (its actual type is generic "UserControl"

    IMyRunControl ctrl = CurrentRightControl as IMyRunControl;
    ctrl.FormButtonClicked();
}

Upvotes: 2

scripni
scripni

Reputation: 2164

The advantage was already pointed out in the link you provided... Basically you can also write

void DoSomething(IMyInterface obj)
{
    obj.DoFirst();
}

And then send any type of object which implements that interface as a parameter.

A myA = new A();
DoSomething(myA);
B myB = new B();
DoSomething(myB);

The method DoSomethig doesn't care about the object's type, as long as it exposes an interface called IMyInterface.

Upvotes: 3

Theodoros Chatzigiannakis
Theodoros Chatzigiannakis

Reputation: 29213

C# is a statically typed language (at least unless you explicitly tell it not to be). This means that the compiler uses the type of the variable to know whether the referenced object has the members you are about to use.

The interface, therefore, provides a contract to the compiler (and to other programmers, too) that this class implements that interface. Because interfaces can be shared across classes that don't have a hierarchical relationship, this means that you can define a method that can take an object as an argument by defining that interface in the parameter type.

Upvotes: 0

Related Questions