1

I want to extend JPanel.

class VisiblePanel extends JPanel{

}

How can I make VisiblePanel call setVisible(true); whenever it is instanciated, without overriding all of the constructors one by one?

WVrock
  • 1,725
  • 3
  • 22
  • 30
  • 1
    If you create a subclass, you have to define all the constructors for that subclass anyway, so you might as well override the ones you plan on using. – DaaaahWhoosh Dec 09 '14 at 18:33

2 Answers2

1

by providing default constructor which invokes this.setVisible(true); and making sure if you overload constructor you still take care of it

jmj
  • 237,923
  • 42
  • 401
  • 438
0

I'm combining the Jigar Joshi's answer and DaaaahWhoosh's comment and adding it some more.

Super class constructors are not inherited so there is no way to call super class constructors. (Java Constructor Inheritance)

So the best way of doing it would be creating a default(without parameters) constructor that does the desired action and calling it from other constructors. If default constructor is required to do something that other constructors shouldn't, creating an initialization method and calling it from every constructor is the way to go.

Community
  • 1
  • 1
WVrock
  • 1,725
  • 3
  • 22
  • 30