aspose file tools*
The moose likes Beginning Java and the fly likes Extend Swing component AND other class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extend Swing component AND other class" Watch "Extend Swing component AND other class" New topic
Author

Extend Swing component AND other class

Dean WinchesterD
Greenhorn

Joined: May 06, 2011
Posts: 13
Hello everybody

I have to types of custom gui components. One extends JLabel while the other one extends JButton. I'd like these two to have exactly the same methods but Java does not allow multiple extends.

The methods are a kind of getters and setters. I'm aware that I can implement extend the JLabel and then implement an interface but I don't want to implement the same method twice.

The implementation does the trick but why bother to implement when I can copy the exact same methods anyway, right?

My problem is more of a structural one. I want to use a 'clean' solution that does not force me to 'copy' the methods in both classes.


Here is what I'd like:



I've tried some things like ClassWithTheMethods extends <T> where T would be JLabel or JButton but I figured generics only go so far for a (good) reason...

Thank you very much.

Dean
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 289

you can make ClassWithTheMethods as abstract class and make a concrete class which is extending the ClassWithTheMethods


-Chiru
Dean WinchesterD
Greenhorn

Joined: May 06, 2011
Posts: 13
Hi CK, I'm not sure I am following.

How can ClassWithTheMethods extend a class that extends JButton or JLabel? Doesn't this mean that I have to create two ClassWithTheMethods-classes?

I'm aware that it is a rather complicated solution for a simple issue, maybe I'm better off the copy the methods anyway.

Your reply got me thinking though, thanks for that.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Welcome to the Ranch

As you know, Java™ only supports single inheritance, not multiple inheritance. In the cases you mention, many of the methods are common to buttons and components. Have a look at the documentation for a button and a label; you will find their inheritance hierarchy at the opt of the page. Look where those inheritance trees diverge, and you can work out which methods they have in common and which are disjoint. Choose whichever has the more disjoint methods (I suspect button) and make you class inherit from that. Then, you can say myNewButton instanceof JButton, but you can't get anything sensible from myNewButton instanceof JLabel.

I would reconsider my design if I found myself facing this sort of problem.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I had the same problem, and it didn't take me long to find out that the common superclass of JLabel and JButton is JComponent. Which implies that the setText() method of JLabel and the setText() method of JButton don't have a common ancestor. That's just convergent evolution, if you like.

My solution was the traditional Java solution; if you want two classes to "have the same methods" and to have that mean something, you create an interface with those methods and have each of the two classes implement that interface.

And the other thing that happened, once I decided to use the interface, was that I realized I didn't need to extend JLabel or JButton either. I realized that composition could work for me just as well as inheritance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extend Swing component AND other class