Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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…

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading