Accessing Superclass Members
your method가 그 superclass의 메소드 중 하나를 오버라이드 한다면, keyword super
를 사용하여 오버라이드된 메소드를 invoke 할 수 있습니다.
또한 감춰진 필드(필드 감추기는 장려되지 않지만)를 참조하기 위하여 super을 사용할 수도 있습니다.
Consider this class, Superclass
:
If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super
. You can also use super
to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass
:
public class Superclass {
public void printMethod() {
System.out.println("Printed in Superclass.");
}
}
Here is a subclass, called Subclass
, that overrides printMethod()
:
public class Subclass extends Superclass {
// overrides printMethod in Superclass
public void printMethod() {
super.printMethod();
System.out.println("Printed in Subclass");
}
public static void main(String[] args) {
Subclass s = new Subclass();
s.printMethod();
}
}
Within Subclass
, the simple name printMethod()
refers to the one declared in Subclass
, which overrides the one in Superclass
. So, to refer to printMethod()
inherited from Superclass
, Subclass
must use a qualified name, using super
as shown. Compiling and executing Subclass
prints the following:
Printed in Superclass. Printed in Subclass
Subclass Constructors
The following example illustrates how to use the super
keyword to invoke a superclass's constructor. Recall from the Bicycle
example that MountainBike
is a subclass of Bicycle
. Here is the MountainBike
(subclass) constructor that calls the superclass constructor and then adds initialization code of its own:
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; }
Invocation of a superclass constructor must be the first line in the subclass constructor.
The syntax for calling a superclass constructor is
super();
super(parameter list);
With super()
, the superclass no-argument constructor is called. With super(parameter list)
, the superclass constructor with a matching parameter list is called.
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.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 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.
'Java 배우기' 카테고리의 다른 글
Writing Final Classes and Methods Final 클래스 및 메소드 작성하기 (0) | 2016.01.05 |
---|---|
Object as a Superclass 수퍼클래스로서의 객체 (0) | 2016.01.05 |
Hiding Fields (0) | 2016.01.05 |
Polymorphism 다형성 (0) | 2016.01.05 |
Overriding and Hiding Methods (0) | 2016.01.05 |
댓글