Super() in IP4AdressImplementation class


I’m tring to understand, why the constructor IP4AdressImplementation() doesn’t work without super(host).

I only know that super(host) will call the parent constructor (in the class Host) that accepts 1 parameter of the type of String (if exists). However, I couldn’t get the purpose of keeping it in constructor.

Here the documentation of super is helpful:

Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.

The call to super ensures that all fields of the super class are correctly initialized such that the subclass instance is a proper instance of the super class.

1 Like

Note that you can also change the argument given to the super() call. For example, if you know that one argument always is "foo", you can call super("foo"). Any expression is allowed there.

1 Like