0

I am going through an example and I am not sure why they are doing this. They are setting mySender to with IEmailSender....

IEmailSender mySender = new MyEmailSender();

why not just write

MyEmailSender mySender = new MyEmailSender();

as MyEmailSender already has the IEmailSender interface reference

Machavity
  • 30,841
  • 27
  • 92
  • 100
datatest
  • 483
  • 1
  • 5
  • 14
  • If later code does not depend on the fact that it's a `MyEmailSender`, that its intent is to simply interact with the interface `IEmailSender` and its members, then it's decoupled from the implementation. It doesn't care about the implementation and cannot accidentally write code depending on it. – Chris Sinclair Jan 30 '13 at 22:26
  • 4
    Programming to an interface allows one to change the implementation more easily (future changes, testing, etc.. – kenny Jan 30 '13 at 22:27
  • http://stackoverflow.com/questions/8531292/why-use-interfaces-multiple-inheritance-vs-interfaces-benefits-of-interfaces – kenny Jan 30 '13 at 22:30

2 Answers2

2

Later in the code they could write

mySender = new MyNewEmailSender();

if necessary. But it isn't necessarily right or wrong. They're just trying to get a point across, mySender is an IEmailSender regardless of the implementation.

Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
  • excellent, I understand. I just can't see myself adding in MyNewEmailSender, it would probably be easier to adjust the one method that needs changing in my MyEmailSender... I am sure there are many cases, just can't think of an example of the top of my head – datatest Jan 30 '13 at 23:25
2

Many reasons, one being if you program against the IEmailSender interface and later decide you want to you MyEvenBetterEmailSender instead, you only have to change one line of code!

Another is it allows you do "decouple" the code from a specific implementation, and perhaps even have it injected instead of hard-coding it within the program.

Read more on interface-based programming on Wikipedia.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Actually, if both implementations really did have the same interface then you'd only need to change that one line of code in either case, you'd just be changing two words on that one line instead of one. – Servy Jan 30 '13 at 22:28
  • True, probably not the best argument, but it's the first that came to mind (and why I added the reference to interface-based programming) – D Stanley Jan 30 '13 at 22:30
  • Well, it's *wrong*, it's not like it's just not important. Even using the concrete type only needs to change one line of code to use a different one. Interfaces are very useful and powerful tools, but when a variable is always and forever a single concrete type, and "you" are the one creating that concrete type, there's very little (if any) advantage to using an interface. Use them when they're appropriate, don't when they're not. – Servy Jan 30 '13 at 22:31
  • @Servy also you're assuming that `IEmailSender` is the _only_ interface that `MyEmailSender` implements. If it implements other interfaces (or has other pure class methods) that are used later on, then just changing the implementation wouldn't work. (Assumiung that the new implementation didn't _also_ have those methods) – D Stanley Jan 30 '13 at 22:34
  • If you were using methods not in that interface then you would *need* to use the concrete type, and it wouldn't be an *option* to use just the interface. – Servy Jan 30 '13 at 23:01