The Use of super()

This week I wrap up my college class on Java programming. We spent a lot of time on some basic Java concepts in class. That meant we had to rush through a lot of the final chapters in the book. Unfortunately we rushed through inheritance, and skipped many sections. One such section we skipped was the use of super() to reach up to a superclass.

On first glass, I assumed you had to prepend any methods of the base class when you call them from a child class. However that is not normally the case. You can just call a method. If it is defined in the super class, Java will know what code to execute normally. That is because subclasses are extensions of the superclass. That is, they have access to the same public methods defined in the superclass.

There are two exceptions which require the use of super. One of them is the constructor of the child class. If you do nothing special in this constructor, Java will call the default constructor of the parent class for you. However if you want to call one of the other constructors of the parent class, you must make that call with super().

The other occasion when you need to use super is when you have a method that is overridden in the child class. If you make a call to an overridden method in the child class, Java chooses the overridden method to execute. You might want to execute the code for the overridden method in the base class. To do that, you have to put the super keyword before the method name.