Calling super() in inheritance — Java

LIU YONGLIANG
4 min readFeb 26, 2021
Photo by Sincerely Media on Unsplash

P.S. Finally back with another quick article on something that I learned during (yet another) late-night discussion.

Motivation

While browsing through the codebase of a brownfield project that I am currently working on as part of my school work, I found the following lines weird at a glance.

public class StorageManager implements Storage {
// other details omitted ...
public StorageManager(AddressBookStorage addressBookStorage, UserPrefsStorage userPrefsStorage) {
super();
this.addressBookStorage = addressBookStorage;
this.userPrefsStorage = userPrefsStorage;
}

The above code belongs to a concrete implementation of an interface named Storage and the particular line that I was interested in is this: super();.

Reasons why I felt that the call to super was unusual:

  1. StorageManager does not extend from any parent class, so I supposed calling super means calling the Object constructor, why would one ever do that?
  2. If somehow the call to super is related to the interface that StorageManager is implementing, it makes no sense because interfaces cannot be instantiated anyway.
  3. While I knew that calling super invokes the parent constructor and one could use it to pass relevant parameters up to the parent, I see no practical purpose in the above code.

Discussion

I posted my question in the class forum and was able to get some helpful responses. So the first thing I did to follow up was to try to remove the super(); to verify whether it has a practical purpose. Tests were still passing and no observable issues when I
restart the application. That proved a point: the call to super, in this case, was unnecessary and meaningless.

I was going to conclude with the above fact and also the practical tip of always testing out code to verify my suspicions. However, my professor’s followed up comment intrigued me. Particularly, this line below did not make sense to me:

An explicit call to super(…) is needed only if the parent class doesn’t have a parameter-less constructor or …

--

--

LIU YONGLIANG

Computer Science Student @ National University of Singapore. Connect with me @ https://www.linkedin.com/in/-yong-/