• 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

usefulness of java interfaces

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the implementor of a Java interface must implement all of the methods in the interface, of what use (other than the possible inheritance of some final private members) is the interface? Why not just provide all of the methods in the class and skip implementing the interface?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To force all of the users that implement the interface to have a consistent implementation. If you make a CAR interface with abstract mehtods for steering, braking, winsheildWiping, etc. then you know that if Jaguar implements CAR it MUST have a method for steering or it is not a CAR. And once I am comfortable with the methods in CAR, I don't have to worry that someone creating a firebird spelled the method differently (like coolSteering) etc.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John,
Only a few weeks ago I too was puzzled about the usefulness of interfaces. It seemed like they were just window-dressing. Now I see them as central and essential to harnessing the power of object-oriented programming.
I'm still learning, and it helps me to relate to someone else my understanding. I would say that the contribution of interfaces tie in very closely with polymorphism, and upcasting. Bruce Eckel has a whole chapter on polymorphism, and it is a great read. Alot of the topics in javaranch are really good to read through as well, to solidify how polymorphism works.
Do you understand the concept of a 'type'? You know how Java is a type-safe language, doing lots of checks for you at compile-time? Well interfaces are key to gaining flexibility you couldn't have otherwise in a type-safe language. Check out a topic I started in this same forum 'Trouble calling a method on a class via Class.forName()'. Also check out Peter Trans' post on Jan 3 to the 'savvy objects' topic in this same forum. His experience with Collection objects was very insightful to me.
If I was further along in my understanding, I could whip off some sample code concisely illustrating the value of multiple inheritance, upcasting, downcasting, etc - but alot of great code examples are already in these forums. Keep looking at it, and at examples in Bruce Eckels Thinking in Java 2nd ed book, and write your own example code. The light will go off, and you will feel like you've had a revelation.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Davis:
If the implementor of a Java interface must implement all of the methods in the interface, of what use (other than the possible inheritance of some final private members) is the interface? Why not just provide all of the methods in the class and skip implementing the interface?



I guess i would tackle your second question first. Why not just provide all of the methods in the class and skip implementing the interface?

1. Once you see that a particular class implements an Interface, you are sure that that class will have a certain set of methods. If two or more classes implement the same interface , you can clearly see the relationship -- this will not be possible if we adopt your approach. For example, just by checking that a class implements the interface Runnable, i can very well decide that it qualifies as a parameter to the Thread constructor. I need not scan the whole code to find whether the run method exists.
2. Let us say your interface has 5 methods and your class implements the interface but needs only 2 of them, you can give an empty body for the rest of the 3 methods,but of course you should define them too. An example might be the implementation of the WindowListener interface. You will not need all the window events. But still, if you implement the WindowListener you have to atleast give a blank body within all the rest of the methods other than the one you need(let us say, Windowclosing event) Fortunately, a WindowAdapter helps out in this case.
3. One more important case in favour of Interfaces is multiple inheritance -- one parent , many children. This can be achieved directly in C++ -- one class can inherit as many classes as it prefers. But in java,a class can extend only one class (singleinheritance). Of course, there will be a lot of instances when we will need the functionality of more than one class. That is when interfaces step in. A class can implement any number of interfaces -- so in effect, you can achieve multiple inheritance .
4. Now to your first question -- the compulsion that you should implement all the methods is a matter of consistency . As has been explained by Cindy in the first response, if the interface is a car -- when you implement the interface, you should implement everything associated with a car,you cannot choose to have the driving() method and omit the startingEngine() method()!!!
Hope this helps,
------------------
Regards,
Shree
[This message has been edited by shree vijay (edited January 04, 2001).]
 
John Davis
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shree Vijay,
I understand your reply but only in a limited fashion with respect to your item #3.
(you say:
3. One more important case in favour of Interfaces is multiple inheritance -- one parent , many children. This can be achieved directly in C++ -- one class can inherit as many classes as it prefers. But in java,a class can extend only one class (singleinheritance). Of course, there will be a lot of instances when we will need the functionality of more than one class. That is when interfaces step in. A class can implement any number of interfaces -- so in effect, you can achieve multiple inheritance .
);
I have difficulty in appreciating this methodology (implementing multiple interfacia) as multiple inheritance, certainly not in the same sense as in which a c++ developer can effect multiple inheritance. After all, a high degree of re-use can be obtained in a real multiple inheritance language vs. a limited degree of re-use in Java via multiple interface implementation. If I understand this correctly, in Java, when I implement multiple interfaces, I am not inheriting the functionality of the methods of multiple classes because I must implement that functionality locally.
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic