File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes generic inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "generic inheritance" Watch "generic inheritance" New topic
Author

generic inheritance

Vasco Carmona
Greenhorn

Joined: Sep 02, 2009
Posts: 11

Hello,

I tried to find this information but I didn't found it.

I would like to make an abstract class called ComponentMouseAdapter that extends any subclass of javax.swing.JComponent and implements all mouse listeners.

Is it possible to have generic inheritance?

I thought that the code would be something like this:


thanks in advance to all replies.


BlackBeltFactory profile
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16687
    
  19


Sorry, Java doesn't have such a feature.... perhaps it would be good to explain what you are trying to do, so that we can suggest an alternative?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32654
    
    4
Maybe ComponentMouseAdapter<E extends MouseListener>??

But there is already a mouseAdapter class and a MouseMotionAdapter class. Why do you need to duplicate them?
Vasco Carmona
Greenhorn

Joined: Sep 02, 2009
Posts: 11

Hello Henry,

thanks for the reply.

I'm building an application that adds labels with images and/or text to a panel at runtime, drags them to wherever we want and change some properties.

For now I just need to insert labels but I'm thinking ahead and trying to do a "universal" inserter for all JComponent subclasses, and all of them to be "dragable" and "changeable". For that I made an interface that as a method called getElement() that return a generic subclass of JComponent.
I could extend the mouse listeners on this interface but I don't want to be able to access directly the mouse listeners methods through objects that implements that interface.
Thus, I wanted to make an abstract class that could extends any subclass of JComponent and make some kind of MouseAdapter, and those that extended this abstract class would override the methods that were needed.

Since it's not possible, I'll make a class that extends JLabel, or any other subclass of JComponent, that implements all those interfaces.

thanks again for the reply.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3791
    
    1

It sounds like what is being attempted is creating components (of arbitrary type) that already have built-in mouse event handling.

This doesn't, to me, seem like a problem that is best solved using inheritance. I'd prefer to use composition. If there really is a need to have the same mouse-event handling on arbitrary JComponents, then it would be simple to inherit from MouseAdapter/MouseMotionAdapter, give the class a reference to a JComponent (passed via the constructor), and then use that reference. Then you just create an instance of this class attached to your component whenever you need it. No generics required.

Edit: I wrote that before I saw Vasco's response. I'd still say the same thing, though.
Vasco Carmona
Greenhorn

Joined: Sep 02, 2009
Posts: 11

What I need is an object that "is-a" JLabel with additional properties.
Since I can only extend from one class, I extend from JLabel and implement the mouse listeners to make my own mouse adapter.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: generic inheritance
 
Similar Threads
Class Design - define methods that all subclasses have in common.
Spring 3
K&B -From genric & collections-Self Test
Spring annotation with inheritance
Initialization with Inheritance