Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Interface variables

Tags:

c#

I'm still trying to get a better understanding of Interfaces. I know about what they are and how to implement them in classes.

What I don't understand is when you create a variable that is of one of your Interface types:

IMyInterface somevariable; 

Why would you do this? I don't understand how IMyInterface can be used like a class...for example to call methods, so:

somevariable.CallSomeMethod(); 

Why would you use an IMyInterface variable to do this?

like image 892
PositiveGuy Avatar asked Jan 28 '10 02:01

PositiveGuy


People also ask

Can we use variables in interface?

Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).

Can interface contains variables Java?

In Java, an interface is an abstract type that contains a collection of methods and constant variables.

Should interface have variables?

You shouldn't put any variables inside Interfaces. Because interfaces define contracts which can be implemented in various ways. The value of a variable is implementation. We certainly can when we know all the classes implementing the interface have some constant variables(Field names for instance).

CAN interface have final variable?

Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static. Since all the methods are abstract you cannot instantiate it.


1 Answers

You are not creating an instance of the interface - you are creating an instance of something that implements the interface.

The point of the interface is that it guarantees that what ever implements it will provide the methods declared within it.

So now, using your example, you could have:

MyNiftyClass : IMyInterface {     public void CallSomeMethod()     {         //Do something nifty     } }  MyOddClass : IMyInterface {     public void CallSomeMethod()     {         //Do something odd     } } 

And now you have:

IMyInterface nifty = new MyNiftyClass() IMyInterface odd = new MyOddClass() 

Calling the CallSomeMethod method will now do either something nifty or something odd, and this becomes particulary useful when you are passing in using IMyInterface as the type.

public void ThisMethodShowsHowItWorks(IMyInterface someObject) {     someObject.CallSomeMethod(); } 

Now, depending on whether you call the above method with a nifty or an odd class, you get different behaviour.

public void AnotherClass() {     IMyInterface nifty = new MyNiftyClass()     IMyInterface odd = new MyOddClass()      // Pass in the nifty class to do something nifty     this.ThisMethodShowsHowItWorks(nifty);      // Pass in the odd class to do something odd     this.ThisMethodShowsHowItWorks(odd);  } 

EDIT

This addresses what I think your intended question is - Why would you declare a variable to be of an interface type?

That is, why use:

IMyInterface foo = new MyConcreteClass(); 

in preference to:

MyConcreteClass foo = new MyConcreteClass(); 

Hopefully it is clear why you would use the interface when declaring a method signature, but that leaves the question about locally scoped variables:

public void AMethod() {     // Why use this?     IMyInterface foo = new MyConcreteClass();      // Why not use this?     MyConcreteClass bar = new MyConcreteClass(); } 

Usually there is no technical reason why the interface is preferred. I usually use the interface because:

  • I typically inject dependencies so the polymorphism is needed
  • Using the interface clearly states my intent to only use members of the interface

The one place where you would technically need the interface is where you are utilising the polymorphism, such as creating your variable using a factory or (as I say above) using dependency injection.

Borrowing an example from itowlson, using concrete declaration you could not do this:

public void AMethod(string input) {                    IMyInterface foo;      if (input == "nifty")     {         foo = new MyNiftyClass();     }     else     {         foo = new MyOddClass();     }     foo.CallSomeMethod(); } 
like image 52
David Hall Avatar answered Sep 23 '22 23:09

David Hall