Home > Net >  Why is it super.paint(g) and not this.paint(g)?
Why is it super.paint(g) and not this.paint(g)?

Time:03-26

Just a theoretical question about inheritance.

So let's say I've got a class "GamePanel" which extends JPanel.

I know within the class, if I call super.paint(g) , it will call the parent class (JPanel) paint method.

But if I create an object GamePanel, shouldn't it already inherit all of JPanels methods? So in that case, why doesn't this.paint(g) work? The current object should be able to access that method right?

If not, why does this.setBackgroundColor(...) work?

Because apparently this.setBackgroundColor(...) works as well as super.setBackGroundColor(...). So it's almost like "super" can access all the methods while "this" can't?

CodePudding user response:

It matters if you've redefined paint in the child class...you need a way within your paint method to call the ancestor paint method, otherwise it could only "call itself" like recursively from within that method...

  • Related