0

I found this code which creates an object of the implementation class and assigning it to the interface class variable:

Myinterface obj=new MyImplementationClass();

Why dont we just use

 MyImplementationClass obj=new MyImplementationClass();

?

Sharun
  • 2,682
  • 5
  • 25
  • 57
  • 6
    How much do you understand about interfaces? It sounds like you might be best off reading an introduction to them. – Jon Skeet May 10 '13 at 11:08
  • 1
    @noobob well, that's *kinda* true - the compiler does let you if you add some COM attributes (to declare the default concrete type) :p – Marc Gravell May 10 '13 at 11:09
  • i guess, it helps in polymorphism, where we can hold objects of different childern classes, and decide the behavior at runtime for each. you can correct me if i'm wrong – Zeeshan May 10 '13 at 11:10
  • @MarcGravell Removed my comment before I see your reply but I didn't know that. Thanks for the info... and respect :) – noobob May 10 '13 at 11:10
  • I am curious who is gonna get the accepted answer :) – noobob May 10 '13 at 11:14
  • Hi all, I have updated my question , and from the answers, what I get is that, if there is only one implementation class, I can simply use `MyImplementationClass obj=new MyImplementationClass();` – Sharun May 10 '13 at 11:14
  • @Sharun if there is only one implementation class, *why is there an interface?* – Marc Gravell May 10 '13 at 11:17
  • @noobob: I have no votes left for today, otherwise would have upvoted all. :) Will do it tomorrow :). Thank you all.. – Sharun May 10 '13 at 11:19
  • @MarcGravell: Ya, then we dont need an interface.. :).. Thank you.. – Sharun May 10 '13 at 11:24
  • 1
    @MarcGravell Where does the 'var' keyword fit into all of this? – John H May 10 '13 at 11:37

2 Answers2

1

There may be more than one class that implements MyInterface. If you use:

MyInterface obj = new MyImplementationClass();

You can also do:

MyInterface obj = new MyOtherImplementationClass();

But if you use the concrete implementation name, you cannot do this (*):

// This wouldn't work:
MyImplementationClass obj = new MyOtherImplementationClass();

And you wouldn't be able to use the following:

MyInterface obj = new MyInterface();

Because you don't know what to insantiate. should it be a MyInterfaceImplementation? Should it be MyOtherInterfaceImplementation?

But there's a technique called Dependency Injection. Which let's you somehow bind a specific implementation to a type. With it, you can do something like the following:

MyInterface obj = dependencyInjectionContainer.Resolve<MyInterface>();

Take a look at What is dependency injection?.

(*) Unless MyOtherImplementationClass inherits from MyImplementationClass.

Community
  • 1
  • 1
hattenn
  • 4,213
  • 7
  • 38
  • 76
1

First you can not create object of Interface. 2nd point using the interface help you to migrate to new class implementation with minimal changes

As in the places of your coding you will work against the Interface not the the class so if tomorrow you have changed the Concrete class , you just need to modify it in one place and all the code will switch to new class.

Devesh
  • 4,132
  • 1
  • 13
  • 25