my dog learned polymorphism
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extend Swing component AND other class" Watch "Extend Swing component AND other class" New topic

Extend Swing component AND other class

Dean WinchesterD

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.

Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 290

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

Dean WinchesterD

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

Joined: Oct 13, 2005
Posts: 46412
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

Joined: Oct 14, 2005
Posts: 19973

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:
subject: Extend Swing component AND other class
It's not a secret anymore!