filmov
tv
Learn Java Programming - Inheritance: Superclass Constructors Tutorial

Показать описание
In my Superclass and Subclass Tutorial I discussed how subclasses inherit the members of a superclass. How does that apply to constructors? Well, constructors are not members, so the subclass doesn't inherit the superclass constructor(s). The subclass can invoke the superclass constructor(s) by using the super keyword. When the new operator initializes an object, it will invoke the proper constructor based on the number and type arguments being passed. The constructor in the subclass can pass its parameters on up the food chain to the superclass constructor. An example program is the best way to demonstrate this principle.