ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 14 TIL
    내일배움캠프 2022. 11. 16. 21:13

     

    강의 듣느 나의 모습

    Java 실무 기초를 듣다가 class 부분이 나오고 상속 등을 이용해서 퀴즈를 풀어봤는데

    알고리즘에서 느꼈던 정답을 보면 이해는되는데 보지 않고 작성을 못하겠네요 .. 

    근데 하다보니 alt + insert > 생성자 or 메서드 등 기본 틀을 직접 치는게 아니라 참 좋네요 ㅎㅎ 

    접근 제어자와 overloding, overriding, 추상을 소개하며 마치겠습니다

    퀴즈는 git hub에 올리는 방법 물어보고 나중에 정리해서 올리겠습니다

     


    접근 제어자
    멤버 변수/함수 혹은 클래스에 사용되며 외부에서의 접근을 제한하는 역할을 합니다.


    private : 같은 클래스 내에서만 접근이 가능합니다
    default(nothing) : 같은 패키지 내에서만 접근이 가능합니다.
    protected : 같은 패키지 내에서, 그리고 다른 패키지의 자손클래스에서 접근이 가능합니다.
    public : 접근 제한이 전혀 없습니다.

     

    mport pkg.ModifierTest;
    
    class Child extends ModifierTest {
        void  callParentProtected() { // void 앞에 아무것도 없기에 private
            System.out.println(" call my parent's protected method");
            super.messageProtected();     // super : 상속받은 부모 클래스를 가리키는 키워드
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            ModifierTest modifierTest = new ModifierTest();
            modifierTest.messageOutside();
    //        modifierTest.messageInside();
    //        modifierTest.messageProtected();
    
            Child child = new Child();
            child.callParentProtected();
    
        }
    }

     

     


     

    overloding

    한 class 내에서 동일한 이름의 method를 여러개 갖음, 이름이 같다고 무조건 overloding은 아니며

    (method 이름 동일, 매개 변수의 개수나 type이 달라야함)

     

    public class Main {
        public static void main(String[] args) {
    
        }
        int add(int x, int y, int z) {
            return x+y+z;
        }
        long add (int a, int b, int c) { // 매개 변수의 개수와 type이 같아서 overloding이 아님
            return a+b+c;
        }
    
        long add (int a, int b, long c) { // 개수를 줄이거나 이 같이 type을 변경해야함 
            return a+b+c;
        }
    }

     

    overriding

    부모 class로부터 상속받은 method를 똑같이 자식 class에 정의 덮음

     

    class Animal {
        String name;
        String color;
    
        public Animal(String name) {
            this.name = name;
        }
    
        public void cry() {
            System.out.println(name + " is caying ");
        }
    }
    
    class Dog extends Animal {
        public Dog(String name) {
            super(name);
        }
    
        public void cry() {
            System.out.println(name + " is barking ");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Animal dog = new Dog("코코");
            dog.cry();
    
        }
    }

     

     


    추상 class

    추상 class란 추상method를 선언할 수 있는 class를 의미

     

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

    '내일배움캠프' 카테고리의 다른 글

    16 TIL // 예비군  (0) 2022.11.18
    15 TIL  (0) 2022.11.17
    13 TIL  (0) 2022.11.15
    12 TIL  (0) 2022.11.14
    11 TIL  (0) 2022.11.11
Designed by Tistory.