Java_정리_03

Jonguk Kim·2021년 11월 6일
0

Java

목록 보기
3/7

1. 접근제어자

  • 접근제어자: 멤버 변수/함수 혹은 클래스에 사용, 외부에서의 접근을 제한하는 역할
    • private: 같은 클래스 내에서만 접근 가능
    • default(nothing): 같은 패키지 내에서만 접근 가능
    • protected: 같은 패키지 내에서, 그리고 다른 패키지의 자손클래스에서 접근 가능
    • public: 접근 제한 전혀 없음
  • Java에서 정확한 클래스의 이름은 package 이름까지 포함한 것이 자바 시스템이 인식하는 클래스의 이름
  • 객체들간의 관계에 따라 접근할 수 있는 권한 구분 필요
  • 클래스 내부 데이터의 부적절한 사용을 보호하기 위해 사용 (캡슐화)
    pkg.ModifierTest 클래스와 pkg2.ModifierTest 는 다른 클래스임
import pkg.ModifierTest;

class Child extends ModifierTest {
    void callParentProtectedMember() {
        System.out.println("Call my parent's protected method");
        super.messageProtected();
    }
}

public class Main {
    public static void main(String[] args) {
        ModifierTest modifierTest = new ModifierTest();

        modifierTest.messageOutside();
//        modifierTest.messageInside(); // compile error
//        modifierTest.messageProtected(); // compile error

        Child child = new Child();
        child.callParentProtectedMember();
    }
}

2. 추상클래스

  • 추상클래스: 추상메소드를 선언할 수 있는 클래스, 상속받는 클래스 없이 그 자체로 인스턴스 생성 불가
  • 추상메소드
    • 설계만 되어있으며 수행되는 코드에 대해서는 작성이 안됨
    • 상속받는 클래스 마다 동작이 달라지는 경우에 상속받는 클래스 작성자가 반드시 작성하도록 하기 위함
      abstract class 클래스이름
      abstract 리턴타입 메스도이름();
abstract class Bird {
    private int x, y, z;

    void fly(int x, int y, int z) {
        printLocation();
        System.out.println("이동합니다.");
        this.x = x;
        this.y = y;
        if (flyable(z)) {
            this.z = z;
        } else {
            System.out.println("그 높이로는 날 수 없습니다");
        }
        printLocation();
    }

    abstract boolean flyable(int z);

    public void printLocation() {
        System.out.println("현재 위치 (" + x + ", " + y + ", " + z + ")");
    }
}

class Pigeon extends Bird {
    @Override
    boolean flyable(int z) {
        return z < 10000;
    }
}

class Peacock extends Bird {
    @Override
    boolean flyable(int z) {
        return false;
    }
}

public class Main {
    public static void main(String[] args) {
        Bird pigeon = new Pigeon();
        Bird peacock = new Peacock();
        System.out.println("-- 비둘기 --");
        pigeon.fly(1, 1, 3);
        System.out.println("-- 공작새 --");
        peacock.fly(1, 1, 3);
        System.out.println("-- 비둘기 --");
        pigeon.fly(3, 3, 30000);
    }
}

3. 인터페이스

  • 인터페이스: 객체의 특정 행동의 특징을 정의
    • 접근제어자, 리턴타입, 메소드 이름만을 정의
    • 함수의 내용 없음
    • 인터페이스를 구현하는 클래스는 인터페이스에 존재하는 함수의 내용({} 중괄호 안의 내용)을 반드시 구현
      interface 인터페이스명{ public abstract void 추상메서드명(); }
interface Bird {
    void fly(int x, int y, int z);
}

class Pigeon implements Bird{
    private int x,y,z;

    @Override
    public void fly(int x, int y, int z) {
        printLocation();
        System.out.println("날아갑니다.");
        this.x = x;
        this.y = y;
        this.z = z;
        printLocation();
    }
    public void printLocation() {
        System.out.println("현재 위치 (" + x + ", " + y + ", " + z + ")");
    }
}

public class Main {

    public static void main(String[] args) {
        Bird bird = new Pigeon();
        bird.fly(1, 2, 3);
//        bird.printLocation(); // compile error
    }
}
  • 인터페이스

    • 구현하려는 객체의 동작의 명세
    • 다중 상속 가능
    • implements를 이용하여 구현
    • 메소드 시그니처(이름, 파라미터, 리턴 타입)에 대한 선언만 가능
  • 추상클래스

    • 클래스를 상속받아 이용 및 확장을 위함
    • 다중 상속 불가능 , 단일 상속
    • extends를 이용하여 구현
    • 추상메소드에 대한 구현 가능
profile
개발일지

0개의 댓글