super 참조변수란 상속구조에서 사용할 수 있는 상위 클래스의 참조 변수
아버지의 클래스로부터 상속을 받았다면 아버지클래스의 모든 것은 아들 클래스 것
아버지의 public이나 protected접근 지정자를 사용하는 멤버에만 접근
버지 클래스와 아들 클래스의 상속관계에서 재정의 메서드가 있고 아들 클래스 내부에서 재정의 된 메서드를 호출한다면, 무조건 아들의 재정의 메서드가 호출
무시되어진 아버지클래스의 메서드를 호출하는 방법은 super참조변수
class NewFather{
public void overrideFunc(){
System.out.println("아버지의 함수");
}
}
public class NewSon extends NewFather{
public void overrideFunc(){
System.out.println("아들의 재정의 함수");
}
public void testFunc(){
super.overrideFunc();
}
public static void main(String[] args){
NewSon s = new NewSon();
s.overrideFunc();//재정의 된 함수호출
s.testFunc();//super를 이용한 아버지 호출
}
}
아들의 재정의 함수
class SuperFather{
private String name;
public SuperFather(String name){
this.name = name;
System.out.println("name:" + name);
}
}
public class SuperSon extends SuperFather{
public SuperSon(String str){
super(str);
}
public static void main(String[] args){
SuperSon s = new SuperSon("상속의 super Test");
}
}
name:상속의 super Test
'컴퓨터 > JAVA' 카테고리의 다른 글
Jsoup로 파싱하기 (0) | 2013.09.05 |
---|---|
강제로 예외 발생시키기 (0) | 2013.08.30 |
ArrayList (0) | 2013.07.05 |
this (0) | 2013.07.05 |
클래스 (0) | 2013.07.05 |