Calling super() in inheritance — Java
4 min readFeb 26, 2021
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:
StorageManager
does not extend from any parent class, so I supposed calling super means calling theObject
constructor, why would one ever do that?- 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. - While I knew that calling super invokes the parent constructor and one could use it to pass…