Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing: Does the phrase "favor composition over inheritance" apply?

Does the phrase "favor composition over inheritance" apply to Swing components? I'd like to gather some professional opinions about the subject and which code is easier to maintain before I go ahead and design a UI.

like image 875
user1329572 Avatar asked Dec 26 '22 21:12

user1329572


1 Answers

Does the phrase "favor composition over inheritance" apply to Swing components?

Yes.

The only time I extend a Swing component is when I need to override one or more of the methods. For instance, I'll extend a JPanel when I want to override the paintComponent method.

All other times, my class will contain the Swing component(s) I need. This allows me to separate my class methods:

frame.getMethod();

from the Swing component class methods:

frame.getFrame().getPreferredSize();
like image 123
Gilbert Le Blanc Avatar answered Dec 29 '22 11:12

Gilbert Le Blanc