ByteBrawl - Constructor Behaviour - Extend Classes - Inheritance

Extend Classes - Constructor Behaviour

As mentioned in Non-Access Modifiers, constructors are not inherited. However, when a subclass is instantiated, the constructor of the superclass is called first to initialize the inherited members. If the superclass has a no-argument constructor (default), it is called automatically. If the superclass does not have a no-argument constructor, the subclass must explicitly call one of the superclass's constructors using the super keyword, or else there will be a compilation error. Here's an example:

Animal.java

Main.java

This shows the advantage of extending classes: we still have the properties of animal but we can add specific functionality only for the Dog class.