Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When your class extends the JFrame class, it inherits some functionality in the

ID: 3681988 • Letter: W

Question

When your class extends the JFrame class, it inherits some functionality in the form of methods like setVisible(), etc. You can normally invoke these methods by either saying:

super.setVisible(true)

or just

setVisible(true)

I.e., it's not mandatory to use the super keyword. But using the super keyword explicitly does offer a few advantages, as well: using the super keyword explicitly makes it clear which class the method you're invoking belongs to. This way, you can easily differentiate between derived functionality/methods and methods developed in the local class. In addition, this approach of explicitly using the super keyword can catch accidental recursive calls, as mentioned here:

Both will work, though; so, let me ask a quick follow-up for everyone: when is it appropriate to use the super keyword and when can you go without it?

Explanation / Answer

When a class extends another class it’s called inheritance. The class that extends is called sub class while the class that is extended is called super class. When a sub class defines a method that has same signature and return type it is called method overriding. Usually, when you call the method simply using the object of type subclass, the overrided method in the subclass will be called. But, still you can access the super class version of the method by using super keyword. So, if you want to exclusively access the method from the super class, then you should use the super keyword, whereas, if you want to access the method from the derived class which is overrided, or if the method in the super class is not overrided in the derived class, in both theses cases you can simply go without it.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote