class Point {
int x;
int z;
String getLocation() {
return "x :"+x+", y : "+y;
}
}
class Point3D extends Point{
int z;
//조상의 getLocation()을 오버라이딩
String getLocation() {
return "x :"+x+", y : "+y+", z : "+z;
}
}
- 선언부가 조상 클래스의 메서드와 일치해야 한다
- 접근 제어자를 조상 클래스의 메서드보다 좁은 범위로 변경할 수 없다
- 예외는 조상 클래스의 메서드보다 많이 선언할 수 있다
❗ 오버로딩과 오버라이딩은 전혀 다른 것이다!
✔오버로딩(overloading) : 기존에 없는 새로운 메서드를 정의하는 것 (new)
✔오버라이딩(overriding) : 상속받은 메서드의 내용을 변경하는 것(change modifiy)
class Parent{
void ParentMethod(){}
}
class Child extends Parent{
void ParentMethod(){} //오버라이딩
void ParentMethod(int i){} //오버로딩
void childMethod(){} //메서드 정의
void childMethod(int i){} //오버로딩
void childMethod(){} //에러! 중복정의
}