반응형
보통 super 클래스를 상속받은 subclass 에서 함수를 overridden 했을경우 supper class 본래
함수를 호출하고 싶을경우와 필드등을 가져오고 싶을때 등 몇가지 사용법이 있다.
http://java.sun.com/docs/books/tutorial/java/IandI/super.html
함수를 호출하고 싶을경우와 필드등을 가져오고 싶을때 등 몇가지 사용법이 있다.
http://java.sun.com/docs/books/tutorial/java/IandI/super.html
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 use super
to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass
:
public class Superclass {Here is a subclass, called
public void printMethod() {
System.out.println("Printed in Superclass.");
}
}
Subclass
, that overrides printMethod()
:
public class Subclass extends Superclass {Within
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 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 thesuper
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) {Invocation of a superclass constructor must be the first line in the subclass constructor.
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}
The syntax for calling a superclass constructor is
super();With
--or--
super(parameter list);
super()
, the superclass no-argument constructor is called. With super(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.Object
does have such a constructor, so ifObject
is 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.반응형
'자바(JAVA)' 카테고리의 다른 글
log4j 서버 재 기동없이 load 되어 적용될수 있게 하자 (1) | 2008.11.30 |
---|---|
velocity 에서 숫자로 for 문을 돌리고자 할때 (0) | 2008.11.30 |
ModelAndViewDefiningException 클래스의 용도 (1) | 2008.11.19 |
Spring 에서 interceptors 사용하기 (0) | 2008.11.19 |
KST 표준시간를 원하는 시간포맷으로 바꾸기 (2) | 2008.10.20 |
새창으로 데이타 submit 해서 넘기기 (0) | 2008.10.11 |
Process 관리하기 (0) | 2008.10.04 |
non-ui thread 에서 main-ui thread 사용시 에러 (0) | 2008.09.28 |