WVrock
WVrock

Reputation: 1765

making a subClass do something whenever any of the super class' constructor is called

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?

Upvotes: 1

Views: 48

Answers (2)

WVrock
WVrock

Reputation: 1765

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.

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240966

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

Upvotes: 1

Related Questions