I usually prefer to follow "composition over inheritance" rule, until I start stepping in the world of Java GUI.
In my experience, for adding custom requirements in a GUI components, for example, add custom function in a JButton, usually create a class extends from JButton like this:
public class CustomButton extends JButton{
public void customMethod(){
}
}
I think it is obviously violating "composition over inheritance" rule, so I try achieving this by composition instead of inheritance, but the problem occurs: JButton has walls of methods, if I use composition, walls of method mapping is required in my CustomButton class:
public class CustomButton{
protected JButton button;
public void customMethod(){
}
public void add(PopupMenu popup){
button.add(popup);
}
public void addActionListener(ActionListener l){
button.addActionListener(l);
}
.
.
.
}
which may require a horrible list of methods to map to JButton.
Also there is one more problem: I don't know how to deal with built-in methods that needs JButton as parameters if my CustomButton is not the subclass of JButton.
So my question is, if the original class has too much methods, is it reasonable to use inheritance instead of composition?