Notice
Recent Posts
Recent Comments
05-21 07:17
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

Byeol Lo

Computer Language - OOP (3) 본문

Programming Language/Java

Computer Language - OOP (3)

알 수 없는 사용자 2023. 9. 15. 16:11

Casting

 클래스 간의 타입을 변경하는 것을 Casting이라고 한다. sub class에서 super class로의 전환 그리고 super class에서 sub class로의 전환이 casting의 예가 되겠다. 여기서 전자를 Upcasting, 후자를 Downcasting이라고 한다.

 

Upcasting 

class Person{...}
class Student extends Person{...}

Student s = new Student();
Person p = s; // Upcasting is occured!

선언해준 p에서 s를 넣어주기만 하면 Upcasting을 쓸 수 있다. 여기서 s라는 Student 인스턴스는 Person의 member를 접근할 수 있게 되지만, Student의 멤버에 접근할 수 없게 된다.

public class tmp {
    public static void main(String args[]) {
        Person p;
        Student s = new Student(2);
        p = s;
        System.out.println(p.name);
        System.out.println(p.old);
        System.out.println(p.grade); // Error
    }
}

class Person{
    String name;
    int old;

    public Person(String name, int old) {
        this.name = name;
        this.old = old;
    }
}

class Student extends Person {
    int grade;

    public Student(int grade) {
        super("student", 20);
        this.grade = grade;
    }
}

 

Downcasting

downcasting은 upcasting과는 다르게 명시적(explicit)으로 코드에 드러난다. Downcasting은 부모 클래스의 인스턴스를 자식 클래스의 인스턴스로 바꾸고 싶을때 사용하면 된다. 예를 들어, 1차 전직(부모 클래스) 한 인스턴스가 후에 2차 전직(자식 클래스)을 하고 싶을 때가 그 예이다.

public class tmp {
    public static void main(String args[]) {
        Person p = new Person("ByeolLo", 24);
        Student s;

        s = (Student) p; // Downcasting is occured
        System.out.println(s.grade);
        System.out.println(s.old);
        System.out.println(s.grade);
    }
}

class Person{
    String name;
    int old;

    public Person(String name, int old) {
        this.name = name;
        this.old = old;
    }
}

class Student extends Person {
    int grade;

    public Student(int grade) {
        super("student", 20);
        this.grade = grade;
    }
}

여기서 downcasting을 한 인스턴스는 부모의 멤버와 자식의 멤버 모두를 사용할 수 있다. 이렇게 부모와 자식간의 변환을 계속해줄때 나중에 해당 변수가 무엇인지 헷갈릴 경우가 있다. 따라서 instanceof의 연산자를 통해 해당 클래스의 instance혹인 자식의 instance인지 확인이 가능하다.

 

Method Overriding

 이제 이를  통해 Polymorphism을 이룰 수 있는데 가령 학생, 선생님, 공무원 등의 사람들이 하는 일들을 보려고 할 때, 다음과 같이 클래스는 다르지만 메소드의 시그니처는 동일하도록 설정하면, Polymorphism을 달성할 수 있다.


public class tmp {
    public static void main(String[] args) {
        Information[] arr = new Information[3];
        arr[0] = new Student();
        arr[1] = new Teacher();
        arr[2] = new PublicOfficial();

        for(int i=0; i<arr.length; i++) {
            arr[i].printInformation();
        }
    }
}

class Information {
    public void printInformation() {
        System.out.println("There is no information.");
    }
}


class Student extends Information{
    public void printInformation() {
        System.out.println("I'm a student.");
    }
}

class Teacher extends Information{
    public void printInformation() {
        System.out.println("I'm a teacher.");
    }
}

class PublicOfficial extends Information{
    public void printInformation() {
        System.out.println("I'm a public official.");
    }
}

여기서 Method Overloading과 Method Overriding을 혼동하지 말자.

'Programming Language > Java' 카테고리의 다른 글

Computer Language - OOP (2)  (0) 2023.09.15
Computer Language - OOP (1)  (0) 2023.05.01
Computer Language - Reference Type  (0) 2023.05.01
Computer Language - Condition & Loop  (2) 2023.04.30
Computer Language - Basic Operators  (2) 2023.03.22
Comments