오버라이딩(overriding)
조상 클래스로부터 상속받은 메서드의 내용을 변경하는 것
상속받은 메서드를 그대로 사용하기도 하지만.. 자손 클래스 자신에 맞게 변경해야 하는 경우가 존재함.
package ch7;
class Point {
int x;
int y;
String getLocation() {
return "x: " + x + ", y: " + y;
}
}
class Point3D extends Point {
int z;
String getLocation() {
return "x: " + x + ", y: " + y + ", z: " + z;
}
}
public class Ex7_0 {
public static void main(String[] args) {
Point p1 = new Point();
Point3D p2 = new Point3D();
p1.x = 3;
p1.y = 4;
p2.x = 3;
p2.y = 4;
p2.z = 5;
System.out.println(p1.getLocation());
System.out.println(p2.getLocation());
}
}
[오버라이딩의 조건]
1. 접근 제어자는 조상 클래스의 메서드보다 좁은 범위로 변경할 수 없다.
만약 조상 클래스에 정의된 메서다ㅡ의 접근 제어자가 protected라면, 오버라이딩하는 자손 클래스의 메서드는 접근 제어자가 protected나 public이어야 한다.
2. 조상 클래스의 메서드보다 많은 수의 예외를 선언할 수 없다.
class Parent {
void parentMethod() throws IOException, SQLException {
...
}
}
//오버라이딩 가능
class Child extends Parent {
void parentMethod() throws IOExcception {
...
}
}
3. 당연한 말이지만.. 선언부가 조상 클래스의 메서드와 일치해야 한다.
오버로딩 VS 오버라이딩
오버로딩: 기존에 없는 새로운 메서드를 정의하는 것
오버라이딩: 싱속받은 메서드의 내용을 변경하는 것.
class Parent {
void parentMethod() { ... }
}
class Child extends Parent {
void parentMethod() { ... } //오버라이딩
void parentMethod(int i) { ... } //오버로딩
void childMethod() { ... }
void childMethod(int i) { ... } //오버로딩
void childMethod() { ... } //에러 발생. 중복 정의
}
참조 변수 super
super는 자손 클래스에서 조상 클래스로부터 상속받은 멤버를 참조하는데 사용되는 참조변수이다.
멤버변수와 자역변수의 이름이 같을 때 this를 붙여서 구별했듯, 상속받은 멤버와 자신의 멤버가 이름이 같을 때 super를 붙여서 구분 가능
package ch7;
class Parent { int x= 10; }
class Child extends Parent {
int x= 20;
void method() {
System.out.println("x = " + x);
System.out.println("this.x = " + this.x);
System.out.println("super.x = " + super.x);
}
}
public class Ex7_2 {
public static void main(String[] args) {
Child c = new Child();
c.method();
}
}
package ch7;
class Parent2 { int x = 10; }
class Child2 extends Parent2{
void method() {
System.out.println("x = " + x);
System.out.println("this.x = " + this.x);
System.out.println("super.x = " + super.x);
}
}
public class Ex7_3 {
public static void main(String[] args) {
Child2 c = new Child2();
c.method();
}
}
Chidl2 클래스의 경우, 상속받은 x 그대로 사용하므로 this.x와 super.x가 같다.
super() - 조상의 생성자
this() 처럼 super()도 생성자이다.
this(): 같은 클래스의 다른 생성자를 호출하는 데 사용.
super(): 조상의 생성자를 호출하는데 사용.
class Point {
int x,y;
Point(int x, int y) {
this.x=x;
this.y=y;
}
}
class Point3d extends Point(){
int z;
Point3D(int x,int y,int z){
this.x=x;
this.y=y;
this.z=z;
}
}
위 코드에서 Point3D 클래스의 생성자가 조상인 Point 클래스로부터 상속받은 x,y를 초기화한다.
틀린 코드는 아니지만, 생성자 Point3D()의 조상 멤버 x,y는 조상의 생성자를 통해 초기화되도록 하는 것이 바람직하다.
class Point {
int x,y;
Point(int x, int y){
this.x = x;
this.y = y;
}
}
class Point3D extends Point {
int z;
Point3D(int x, int y, int z){
super(x,y);
this.z = z;
}
}
public class Ex7_4 {
public static void main(String[] args) {
Point3D p = new Point3D(1,2,3);
System.out.println("x = " + p.x + ", y = " + p.y + ", z = " + p.z);
}
}
'프로그래밍 > java' 카테고리의 다른 글
[JAVA] 다형성(polymorphism) (0) | 2022.08.28 |
---|---|
[JAVA] 패키지와 import, 제어자, 겟터와 셋터 (0) | 2022.08.28 |
[JAVA] 상속 (0) | 2022.08.26 |
[JAVA] 객체, 인스턴스, 클래스, 메서드, 변수, static (0) | 2022.08.26 |
[JAVA] 2차원 배열과 초기화 (0) | 2022.08.25 |