오버로딩 → 기존에 없는 새로운 메서드를 추가 (new)

오버라이딩 → 조상으로부터 상속받은 메서드의 내용을 변경 (change, modify)

class Child extends Parent{
    int x = 20;

    void parentMethod() {   // 오버라이딩

    }

    void parentMethod(int i) {  // 오버로딩

    }

    void childMethod() {}
    void childMethod(int i) {}  // 오버로딩
//    void childMethod() {} // -> 에러. 중복 정의.

    void method() {
       System.out.println("x=" + x);
        System.out.println("this.x=" + this.x);
        System.out.println("super.x=" + super.x);
    }
}

super

super는 자손 클래스에서 조상 클래스로부터 상속 받은 멤버를 참조하는데 사용되는 참조변수이다.

static 메서드는 인스턴스와 관련이 없다. 그래서 this와 마찬가지로 super 역시 static 메서드에서는 사용할 수 없고 인스턴스 메서드에서만 사용할 수 있다.

public class SuperTest {
    public static void main(String[] args) {
        Child2 c = new Child2();
        c.method();
    }
}

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);
    }
}

x, this.x, super.x 가 모두 같은 변수를 의미하므로 같은 값이 출력된다.

public class SuperTest2 {
    public static void main(String[] args) {
        Child3 c = new Child3();
        c.method();
    }
}

class Parent3 {
    int x = 10;
}

class Child3 extends Parent3 {
    int x = 20;

    void method() {
        System.out.println("x = " + x);
        System.out.println("this.x = " + this.x);
        System.out.println("super.x = " + super.x);
    }
}

x = 10, this.x = 20, super.x = 10 이 출력된다

class Point {
    int x;
    int y;

    String getLocation() {
        return "x :" + x + ", y :" + y;
    }
}

class Point3D extends Point {
    int z;
    String getLocation() {
        return super.getLocation() + ", z :" + z;
    }
}