• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

what r interfaces for

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interfaces dont allow u to declare protected methods
and after implemeting u have to follow the signature
so implemetation has to have all those methods and the derived classes cannot restrict visibility
so that means that if u have a number of classes with a lot in common who have proteceted visibilty u can never define the coomon protected members that in the intrface
a great setback
so what is the whole idea of restricting the interface to only public stuff??
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface is designed to describe the public appearance of a set of classes. Look at the Collection or Set interfaces. Or even better, look at the interfaces that describe the public appearance of JDBC or JMS.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've personally seen about 3 or 4 different posts asking what interfaces are for in just two days. I'm not frustrated, but a good resource would be to do a search on the forums.
I just think it would be a good idea to consolidate these separate questions in one area...kinda like code reuse you know. Reuse the comments made before.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas (or anyone who knows for certain), I think there is some legitimacy to this question. One could ask "Why should I have to implement the MouseListener (for instance) and provide stub implementations for 5 methods when the only one I care about is MouseClick? Why can't I just write my own MouseClick method in my class and have it perform the same function?"
I believe the answer is "hidden". I think that the MouseListener interface actually is doing stuff completely behind the scenes in order to communicate with the mouse device drivers at a low level. This is functionality that we can't code with casual Java and which is invisible to programmers simply perusing the API. Perhaps there are native methods being called which aren't publicly documented?
Someone should confirm or deny the above. If it's accurate, then there is a significant difference between the interfaces provided with the JDK and those which we are allowed to write ourselves. From a purely functional point of view (ignoring all principles of good coding and OO architecture), it should be possible to achieve any desired behavior without ever defining an interface.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Scott Appleton:
I believe the answer is "hidden". I think that the MouseListener interface actually is doing stuff completely behind the scenes in order to communicate with the mouse device drivers at a low level. This is functionality that we can't code with casual Java and which is invisible to programmers simply perusing the API. Perhaps there are native methods being called which aren't publicly documented?


No, this is not true. The reason is simpler. In order to register to hear mouse events, you must ask the component generating those events to let you know when the event occurs. How does it notify you? It does so by calling a method in your code. How does it know what method to call? It calls the method defined in the interface that you have implemented. There is nothing secret or tricky going on under the covers. It is actually very simple.

------------------
Co-Moderator of the Programmer Certification Forums
 
Sheriff
Posts: 15995
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Appleton:
I believe the answer is "hidden". I think that the MouseListener interface actually is doing stuff completely behind the scenes...
Seems to me you've got it backwards. The MouseListener interface does not do anything behind the scenes--in fact, it doesn't do anything at all since it's just an interface.
Interfaces are a "higher" form of abstraction than abstract classes. They allow for greater flexibility and loose coupling. In Effective Java, author Joshua Bloch recommends that you favor interfaces over abstract classes and gives several reasons for doing so. He does say that public interfaces must be designed more carefully and that you need to get it right before publishing it because an interface is much harder to evolve than an abstract class.
 
MrShahid khan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Why should I have to implement the MouseListener (for instance) and provide stub implementations for 5 methods when the only one I care about is MouseClick? Why can't I just write my own MouseClick method in my class and have it perform the same function?"

well if only need the mouse click event why do'r u use the adapter classes
they are ther exaxctly for the purpose u r saying

[This message has been edited by MrShahid khan (edited July 13, 2001).]
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should I have to use an adapter class at all? Why can't I write my own interface that declares a mouseClick method, implement it in my class declaration, code "button.addMyMouseListener(this)" and write a mouseClick method in my class that implements the mouseClick method from my interface? If there's nothing "special" about the MouseListener interface, then I should be able to do that and have it work.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because there is no method of Button called addMyMouseListener(). So you would have to write your own Button class as well.
The addMouseListener() method of Button takes an object of type MouseListener as a parameter.
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I guess I would have to create a new class that extends Button and write an addMyListener method -- except that I don't know how the Button class in java.awt implements its addActionListener method, so I wouldn't know how to write such a method. Perhaps its not the Listener interface itself, but rather the addXXXListener methods defined for awt components which hold the key to this?
I've written quite a few awt GUIs and have a good understanding of how to make this stuff work using the supplied Java classes. I'm just searching for "deeper" meaning by trying to ascertain exactly where the code is that interfaces directly with the OS input devices.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code that interacts with the underlying OS is in the peers. It is written in native code. If you are interested then I would suggest you look at the source code.
The addActionListener method simply adds the passed object to a Vector. When the button is pressed, the button object goes through the Vector and runs the actionPerformed method of every object in the Vector. Since every object in the Vector must be an ActionListener, they all have an actionPerformed method.
 
She'll be back. I'm just gonna wait here. With this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic