• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using interface as a type?

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings, I would appreciate if someone could explain the purpose of using interface as a type. And why is it legal? I find it quite confusing, perhaps there is a better example to use interface as a type?

 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would prefer to see the method called launchRocket().
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

* Using interface as a type. What is the whole
point of this, why is this legal? Does it
make any sense?*/




Haven't you read the chapters "Inheritance" and "Polymorphism" yet?
 
Randy Smith
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I would prefer to see the method called launchRocket().



when both Car and FighterPlane implement Weapons, all Cars and FighterPlanes can launch rockets, why do we need to use Weapon as a type?
 
Randy Smith
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harsha Smith wrote:

* Using interface as a type. What is the whole
point of this, why is this legal? Does it
make any sense?*/




Haven't you read the chapters "Inheritance" and "Polymorphism" yet?



yes, I have read them, but I don't know understand the purpose of using interface as a reference type. objects created using interface type can't access methods of the class that implements it. I was thinking objects of interface type should have additional methods instead, but it is the other way around.
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randy Smith wrote:

Campbell Ritchie wrote:I would prefer to see the method called launchRocket().



when both Car and FighterPlane implement Weapons, all Cars and FighterPlanes can launch rockets, why do we need to use Weapon as a type?


Try rewriting that code without using Weapon as a type.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be easier to understand if you change “Weapon” to “RocketCarrier” throughout. Similarly change “Car” to “ArmouredCar”; most Cars don’t carry rockets!
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An interface is a specification (or contract) that tells the caller that the implementing type has the capability to do something.

A good example is if we take a Person that implements the interfaces HardWorker and GreatLover.

As your boss, I do care about the methods that you implements according to the HardWorker interface. But I don't really care about your other implementing interfaces.

As your partner, I don't like that your boss is calling the methods in the HardWorker interface since that takes away the possibility for me to call the GreatLover-methods. And some stupid programmer has put a synchronization on the implementing methods.

So, depending on who is using the implementing type, they want to know that you can do what your interface/specification promises.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randy Smith wrote:Greetings, I would appreciate if someone could explain the purpose of using interface as a type...


I'll leave the "why is it legal" to someone else, because almost always the answer to questions like that is "because the Java designers thought it was a good idea".

And it is. Here's a real-life example:I've just written a method that works with 4 different classes (and probably many more), simply because instead of defining its parameter as a class, I defined it as an interface; and because I did that, it'll work with any class that implements List (or, in the above case, List<String>).

And when you define a field (eg, 'al' above), if you define it using the interface type, you can change your mind later. Assuming that all my methods are written to take Lists, rather than ArrayLists, I can change the above definition to:
List<String> al = new AttributeList<String>();
and everything will continue to work just fine.

HIH

Winston
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should also remember that large projects are developed by teams. For example, team A develops the client, and team B develops the back-end server.

Team B says "We will create a widget object that implements the fubar interface.".

Team A can do one of two things. They can code to the specific type of object (widget) that actually gets created, or they can code to the interface given (fubar).

Let's say they code to the specific type of widget. a year later, just two days before go-live, Team B says "We need to change the object we are giving you. It won't be a widget, but a dohicky, which also implements the fubar interface.".

Team A is screwed. They need to go through every line of code and change it to use the dohicky type.

If only they had used the fubar interface, they WOULDN'T HAVE TO TOUCH A LINE of code - because any dohicky can be assigned to a fubar interface type.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic