a portion of a Shape class hierarchy. Shee fWoDimensicnalshere Cindle srne Triable Ghe Tetrahedron Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 11 A portion of a Shape class hierarchy. Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube Tetrahedron
Using the Keyword super The keyword super refers to the superclass of the class in which super appears. This keyword can be used in two ways o To call a superclass constructor o To call a superclass method Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 12 Using the Keyword super ⚫ To call a superclass constructor ⚫ To call a superclass method The keyword super refers to the superclass of the class in which super appears. This keyword can be used in two ways:
CAUTION Ouse the keyword super to call the superclass constructor Invoking a superclass constructors name in a subclass causes a syntax error OJava requires that the statement that uses the keyword super appear first in the constructor Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 13 CAUTION ⚫use the keyword super to call the superclass constructor. Invoking a superclass constructor’s name in a subclass causes a syntax error. ⚫Java requires that the statement that uses the keyword super appear first in the constructor
NOTE OUnlike properties and methods, a superclass's constructors are not inherited in the subclass O They can only be invoked from the subclasses constructors, using the keyword super olf the keyword super is not explicitly used, the superclass' s no-arg constructor is automatically invoke Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 14 NOTE ⚫Unlike properties and methods, a superclass's constructors are not inherited in the subclass. ⚫They can only be invoked from the subclasses' constructors, using the keyword super. ⚫If the keyword super is not explicitly used, the superclass's no-arg constructor is automatically invoked
Superclass's Constructor Is Always Invoked A constructor may invoke an overloaded constructor or its superclass's constructor. If none of them is invoked explicitly, the compiler puts super( as the first statement in the constructor. For example, public Cylinder() i is equivalent to public Cylinder() i super ( public A(double d) public a(double d)( // some statements is equivalent to super()i // some statements Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 15 Superclass’s Constructor Is Always Invoked A constructor may invoke an overloaded constructor or its superclass’s constructor. If none of them is invoked explicitly, the compiler puts super( ) as the first statement in the constructor. For example, public Cylinder() { } is equivalent to public Cylinder() { super(); } public A(double d) { // some statements } is equivalent to public A(double d) { super(); // some statements }