본문 바로가기
Java 강의

자바 동적 바인딩 / 자바 다형성

by 자유코딩 2017. 10. 13.

안녕하세요 이번 글에서는 자바 동적 바인딩과 다형성에 대해서 알아보도록 하겠습니다.

 

1
2
3
4
5
public class BookStore {
    public void superprint() {
        System.out.println("가져다 쓰세요");
    }
}
cs

 

1
2
3
4
5
6
7
8
public class Web extends BookStore{
    @Override
    //가져다 쓰자
    public void superprint() {
        System.out.println("web이다");
        // TODO Auto-generated method stub
    }
}
cs

 

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
public class Java extends BookStore{
    public static void main(String[] args) {
        Java j1 = new Java();
        Web w1 = new Web();
        BookStore[] book = new BookStore[2];
        book[0= j1;
        book[1= w1;
        
        book[0].superprint();
        book[1].superprint();
        
        j1.superprint();//상속을 받게 되면 자식 클래스의 인스턴스가 
        //부모 클래스의 함수와 데이터를 사용 할 수 있게 된다
        j1.print();
    }
    public void print() {
        System.out.println("자바다");
    }
    @Override
    //가져다 쓰자
    public void superprint() {
        // TODO Auto-generated method stub
        super.superprint();
        System.out.println("자바다");
    }
}
cs

 

다른 예제도 보겠습니다

 

1
2
3
4
5
6
7
public class Circle extends Shape{
    @Override
    public void draw() {//상위 클래스 Shape의 메소드 draw를 오버라이드 합니다
        // TODO Auto-generated method stub
        System.out.println("원을 그립니다");
    }
}
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Rectangle extends Shape{
    private int width;
    private int height;
    @Override
    public void draw() {//상위 클래스 Shape의 메소드를 오버라이드 합니다.
        // TODO Auto-generated method stub
        System.out.println("사각형을 그립니다");
    }
    public int getWidth() {
        return width;
    }
    public void setWidth(int width) {
        this.width = width;
    }
    public int getHeight() {
        return height;
    }
    public void setHeight(int height) {
        this.height = height;
    }    
}
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class ShapeTest {
    public static void main(String[] args) {
        Rectangle rec = new Rectangle();
        Circle cir = new Circle();
        Shape s = new Rectangle();
        Shape s1 = new Circle();
        
        Shape[] shape = new Shape[3];
        //동일한 객체 shape에 draw라는 동일한 명령을 내렸다.
        //그런데 shape[0],shape[1],shape[2] 각각에 Rectangle, Circle을 달리 대입했다.
        //그러므로 이들은 각자 다른 동작을 한다.
        //이렇게 동일한 객체 shape가 각자 다른 동작을 하는 것을 다형성이라고 한다.
        shape[0= new Rectangle();
        shape[1= new Circle();
        shape[2= new Rectangle();
        for (int i = 0; i < shape.length; i++) {
            shape[i].draw();//동적 바인딩 : 프로그램의 시작에서 shape[0],shape[1],shape[2]이 어떤 draw를 하는지 모른다
            //이 for문 안에서 i가 바뀌면서 draw를 붙여주고 실행한다. 이렇게 어떤 클래스의 메소드를 수행할지 프로그램이 실행되면서 결정되는것이다.
            //이것이 동적 바인딩이다.
        }    
    }
}
cs

 

코드에 주석으로 설명을 적었습니다.

 

동적 바인딩

코드를 보시면 프로그램이 시작될 때 shape[0],shape[1],shape[2] 가 각각 힙영역에서 어떤 클래스를 활용할지 정해지지 않았습니다.

shape[0] , shape[1] , shape[2] 이 각각 Rectangle , Circle 다른 객체를 참조하게 만듭니다.

 

그리고 for문에서 shape[0] , shape[1] , shape[2]으로 draw메소드를 호출합니다

이렇게 하면 Shape[] shape = new Shape[3];에서 생성된 shape객체가 각기 다른 draw동작을 수행하게 됩니다.

프로그램이 진행되면서 shape객체의 동작이 정해지는 것입니다.

 

이렇게 프로그램이 진행되면서 객체의 동작이 정해지는 것을 동적 바인딩이라고 합니다.

 

 

다형성

 

그리고 이 프로그램에서는 shape라는 객체가 각자 할당된 다른 동작을 수행하게 됩니다.

이렇게 같은 객체가 다른 동작을 수행 할 수 있는 것을 다형성이라고 합니다.

 

이번 글에서는 동적 바인딩과 다형성에 대해서 알아보았습니다.

jswoo030@gmail.com 으로 질문을 보내시면 빠른 답변을 받으실 수 있습니다.

 

 

댓글