Accessing Superclass Members
If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keywordsuper. You can also usesuperto refer to a hidden field (although hiding fields is discouraged). Consider this class,Superclass:Here is a subclass, calledpublic class Superclass { public void printMethod() { System.out.println("Printed in Superclass."); } }Subclass, that overridesprintMethod():Withinpublic class Subclass extends Superclass { public void printMethod() { //overrides printMethod in Superclass super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(); s.printMethod(); } }Subclass, the simple nameprintMethod()refers to the one declared inSubclass, which overrides the one inSuperclass. So, to refer toprintMethod()inherited fromSuperclass,Subclassmust use a qualified name, usingsuperas shown. Compiling and executingSubclassprints the following:Printed in Superclass. Printed in SubclassSubclass Constructors
The following example illustrates how to use thesuperkeyword to invoke a superclass's constructor. Recall from theBicycleexample thatMountainBikeis a subclass ofBicycle. Here is theMountainBike(subclass) constructor that calls the superclass constructor and then adds initialization code of its own:Invocation of a superclass constructor must be the first line in the subclass constructor.public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; }The syntax for calling a superclass constructor is
Withsuper(); --or-- super(parameter list);super(), the superclass no-argument constructor is called. Withsuper(parameter list), the superclass constructor with a matching parameter list is called.If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of
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.Objectdoes have such a constructor, so ifObjectis the only superclass, there is no problem.Object. In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent.
ATENCIÓN: La traducción de esta documentación es un esfuerzo personal y voluntario. NO es un documento oficial del propietario de la tecnología Java, Oracle, ni está patrocinado por esta empresa.
Los documentos originales y actualizados (en inglés) están disponibles en: http://docs.oracle.com/javase/tutorial/. La versión disponible en este sitio es la publicada en Marzo de 2008 (más información en: "What's new and What's Old? The History of the Tutorial").
Dirige cualquier comentario, petición, felicitación, etc. a tutorialesjava@codexion.com.
Si quieres ayudar a mantener en funcionamiento esta web, colaborar con la traducción de estos documentos o necesitas que se traduzca algún capítulo en concreto puedes invitarme a un café: