[JAVA]
다형성(polymorphism)이란?
다형성(polymorphism)이란 하나의 객체가 여러 가지 타입을 가질 수 있는 것을 의미합니다
자바에서는 이러한 다형성을 부모 클래스 타입의 참조 변수로 자식 클래스 타입의 인스턴스를 참조할 수 있도록 하여 구현하고 있습니다
다형성은 상속, 추상화와 더불어 객체 지향 프로그래밍을 구성하는 중요한 특징 중 하나입니다
참조 변수의 다형성
자바에서는 다형성을 위해 부모 클래스 타입의 참조 변수로 자식 클래스 타입의 인스턴스를 참조할 수 있도록 하고 있습니다
이때 참조 변수가 사용할 수 있는 멤버의 개수가 실제 인스턴스의 멤버 개수보다 같거나 적어야 참조할 수 있습니다
다음 예제는 참조 변수의 다형성을 보여주는 예제입니다
예제
class Parent { ... }
class Child extends Parent { ... }
...
Parent pa = new Parent(); // 허용
Child ch = new Child(); // 허용
Parent pc = new Child(); // 허용
Child cp = new Parent(); // 오류 발생
특정 타입의 참조 변수로는 당연히 같은 타입의 인스턴스를 참조할 수 있습니다
참조 변수가 사용할 수 있는 멤버의 개수가 실제 인스턴스의 멤버 개수와 같기 때문입니다
그리고 부모 클래스 타입의 참조 변수로도 자식 클래스 타입의 인스턴스를 참조할 수 있습니다
참조 변수가 사용할 수 있는 멤버의 개수가 실제 인스턴스의 멤버 개수보다 적기 때문입니다
하지만 반대의 경우인 자식 클래스 타입의 참조 변수로는 부모 클래스 타입의 인스턴스를 참조할 수 없습니다
참조 변수가 사용할 수 있는 멤버의 개수가 실제 인스턴스의 멤버 개수보다 많기 때문입니다
클래스는 상속을 통해 확장될 수는 있어도 축소될 수는 없으므로, 자식 클래스에서 사용할 수 있는 멤버의 개수가 언제나 부모 클래스와 같거나 많게 됩니다
참조 변수의 타입 변환
자바에서는 참조 변수도 다음과 같은 조건에 따라 타입 변환을 할 수 있습니다
-
서로 상속 관계에 있는 클래스 사이에만 타입 변환을 할 수 있습니다
-
자식 클래스 타입에서 부모 클래스 타입으로의 타입 변환은 생략할 수 있습니다
-
하지만 부모 클래스 타입에서 자식 클래스 타입으로의 타입 변환은 반드시 명시해야 합니다
참조 변수의 타입 변환도 기본 타입의 타입 변환과 마찬가지로 타입 캐스트 연산자(())를 사용합니다
문법
(변환할 타입의 클래스 이름) 변환할 참조변수
다음 예제는 참조 변수의 타입 변환을 보여주는 예제입니다
예제
class Parent { ... }
class Child extends Parent { ... }
class Brother extends Parent { ... }
...
Parent pa01 = null;
Child ch = new Child();
Parent pa02 = new Parent();
Brother br = null;
pa01 = ch; // pa01 = (Parent)ch; 와 같으며, 타입 변환을 생략할 수 있음
br = (Brother)pa02; // 타입 변환을 생략할 수 없음
br = (Brother)ch; // 직접적인 상속 관계가 아니므로, 오류 발생
instanceof 연산자
이러한 다형성으로 인해 런타임에 참조 변수가 실제로 참조하고 있는 인스턴스의 타입을 확인할 필요성이 생깁니다
자바에서는 instanceof 연산자를 제공하여, 참조 변수가 참조하고 있는 인스턴스의 실제 타입을 확인할 수 있도록 해줍니다
자바에서 instanceof 연산자는 다음과 같이 사용합니다
문법
참조변수 instanceof 클래스이름
왼쪽에 전달된 참조 변수가 실제로 참조하고 있는 인스턴스의 타입이 오른쪽에 전달된 클래스 타입이면 true를 반환하고, 아니면 false를 반환합니다
만약에 참조 변수가 null을 가리키고 있으면 false를 반환합니다
다음 예제는 참조 변수가 실제로 가리키고 있는 인스턴스의 타입을 instanceof 연산자로 확인하는 예제입니다
예제
class Parent { }
class Child extends Parent { }
class Brother extends Parent { }
public class Polymorphism01 {
public static void main(String[] args) {
Parent p = new Parent();
**System.out.**println(p instanceof Object); // true
**System.out.**println(p instanceof Parent); // true
**System.out.**println(p instanceof Child); // false
**System.out.**println();
Parent c = new Child();
**System.out.**println(c instanceof Object); // true
**System.out.**println(c instanceof Parent); // true
**System.out.**println(c instanceof Child); // true
****}
}
추상 클래스(abstract class)
자바에서는 하나 이상의 추상 메소드를 포함하는 클래스를 가리켜 추상 클래스(abstract class)라고 합니다
이러한 추상 클래스는 객체 지향 프로그래밍에서 중요한 특징인 다형성을 가지는 메소드의 집합을 정의할 수 있도록 해줍니다
즉, 반드시 사용되어야 하는 메소드를 추상 클래스에 추상 메소드로 선언해 놓으면, 이 클래스를 상속받는 모든 클래스에서는 이 추상 메소드를 반드시 재정의해야 합니다
자바에서 추상 클래스는 다음과 같은 문법으로 선언합니다
문법
abstract class 클래스이름 {
...
abstract 반환타입 메소드이름();
...
}
이러한 추상 클래스는 동작이 정의되어 있지 않은 추상 메소드를 포함하고 있으므로, 인스턴스를 생성할 수 없습니다
추상 클래스는 먼저 상속을 통해 자식 클래스를 만들고, 만든 자식 클래스에서 추상 클래스의 모든 추상 메소드를 오버라이딩하고 나서야 비로소 자식 클래스의 인스턴스를 생성할 수 있게 됩니다
추상 클래스는 추상 메소드를 포함하고 있다는 점을 제외하면, 일반 클래스와 모든 점이 같습니다.즉, 생성자와 필드, 일반 메소드도 포함할 수 있습니다
예제
abstract class Animal { abstract void cry(); }
class Cat extends Animal { void cry() { **System.out.**println("냐옹냐옹!"); } }
class Dog extends Animal { void cry() { **System.out.**println("멍멍!"); } }
public class Polymorphism02 {
public static void main(String[] args) {
****// Animal a = new Animal(); // 추상 클래스는 인스턴스를 생성할 수 없음.
Cat c = new Cat();
Dog d = new Dog();
**** c**.**cry();
**** d**.**cry();
****}
}
A클래스, B클래스, C클래스가 있다고 가정해봅시다. 여기서 각 클래스 안에는 각자의 필드와 메서드가 있을것이다. 추상클래스는 A클래스, B클래스, C클래스 간에 비슷한 필드와 메서드를 공통적으로 추출해 만들어진 클래스입니다
예를 들어 '키보드'라는 클래스가 있습니다. '키보드'를 만드는 제조사는 여러개입니다. A제조사, B제조사, C제조사는 각 제조사만의 스타일대로 키보드를 제작하고 소비자들에게 제품을 출시합니다. 여기서 A제조사는 키보드를 누를때 마다 불빛이 들어온다. B제조사는 키보드를 누를때 딸깍 거리는 소리가 납니다. C제조사는 키보드를 누를때 살짝만 눌러도 잘 눌립니다
각기 다른 특성을 지닌 세 키보드 간에도 공통적인 부분이 있습니다. 키보드를 눌러서 사용하는 것입니다. 이것은 모두 공통입니다. 즉 메서드가 공통적입니다. 그럼 이 메서드를 추출해서 추상클래스 안에 두면 됩니다
(키보드를 상속받아 탄생한, A키보드, B키보드, C키보드)
다른 예로는 사람의 얼굴이 있습니다. 일반적으로 귀 2개, 눈 2개, 입 1개, 코 1개를 지니고있습니다
이는 모두 공통적인 변수입니다. 이 변수들을 추상클래스에 넣는 것입니다
실체클래스는 실체가 드러나는 클래스이고, 추상클래스는 실체 클래싀의 공통적인 부분을 추출하여 어느 정도 규격을 잡아놓은 추상적인 클래스입니다
실체클래스는 실체 객체를 생성할 정도의 구체성을 가지고 있는 반면, 추상클래스는 아직 메서드와 내용이 추상적이기 때문에 객체를 생성할 수 없게 만들었습니다
따라서 객체를 직접 생성할 수 있는 클래스를 실체클래스라고 하고, 실체클래스들의 공통적인 특성을 추출해서 선언한 클래스를 추상클래스라고 합니다. 여기서 추상클래스와 실체클래스는 상속적인 관계를 가지고 있습니다
1. 추상클래스란 실체클래스의 공통적인 부분(변수,메서드)를 추출해서 선언한 클래스
2. 추상클래스는 객체를 생성할 수 없음. 아직은 실체성이 없고 구체적이지 않기 때문
3. 추상클래스와 실체클래스는 상속관계
추상 메소드(abstract method)
추상 메소드(abstract method)란 자식 클래스에서 반드시 오버라이딩해야만 사용할 수 있는 메소드를 의미합니다
자바에서 추상 메소드를 선언하여 사용하는 목적은 추상 메소드가 포함된 클래스를 상속받는 자식 클래스가 반드시 추상 메소드를 구현하도록 하기 위함입니다
예를 들면 모듈처럼 중복되는 부분이나 공통적인 부분은 미리 다 만들어진 것을 사용하고, 이를 받아 사용하는 쪽에서는 자신에게 필요한 부분만을 재정의하여 사용함으로써 생산성이 향상되고 배포 등이 쉬워지기 때문입니다
이러한 추상 메소드는 선언부만이 존재하며, 구현부는 작성되어 있지 않습니다
바로 이 작성되어 있지 않은 구현부를 자식 클래스에서 오버라이딩하여 사용하는 것입니다
자바에서 추상 메소드는 다음과 같은 문법으로 선언합니다
문법
abstract 반환타입 메소드이름();
인터페이스(interface)란?
자식 클래스가 여러 부모 클래스를 상속받을 수 있다면, 다양한 동작을 수행할 수 있다는 장점을 가지게 될 것입니다
하지만 클래스를 이용하여 다중 상속을 할 경우 메소드 출처의 모호성 등 여러 가지 문제가 발생할 수 있어 자바에서는 클래스를 통한 다중 상속은 지원하지 않습니다
하지만 다중 상속의 이점을 버릴 수는 없기에 자바에서는 인터페이스라는 것을 통해 다중 상속을 지원하고 있습니다
인터페이스(interface)란 다른 클래스를 작성할 때 기본이 되는 틀을 제공하면서, 다른 클래스 사이의 중간 매개 역할까지 담당하는 일종의 추상 클래스를 의미합니다
자바에서 추상 클래스는 추상 메소드뿐만 아니라 생성자, 필드, 일반 메소드도 포함할 수 있습니다
하지만 인터페이스(interface)는 오로지 추상 메소드와 상수만을 포함할 수 있습니다
인터페이스의 선언
자바에서 인터페이스를 선언하는 방법은 클래스를 작성하는 방법과 같습니다
인터페이스를 선언할 때에는 접근 제어자와 함께 interface 키워드를 사용하면 됩니다
자바에서 인터페이스는 다음과 같이 선언합니다
문법
접근제어자 interface 인터페이스이름 {
public static final 타입 상수이름 = 값;
...
public abstract 메소드이름(매개변수목록);
...
}
단, 클래스와는 달리 인터페이스의 모든 필드는 public static final이어야 하며, 모든 메소드는 public abstract이어야 합니다
이 부분은 모든 인터페이스에 공통으로 적용되는 부분이므로 이 제어자는 생략할 수 있습니다
이렇게 생략된 제어자는 컴파일 시 자바 컴파일러가 자동으로 추가해 줍니다
인터페이스의 구현
인터페이스는 추상 클래스와 마찬가지로 자신이 직접 인스턴스를 생성할 수는 없습니다
따라서 인터페이스가 포함하고 있는 추상 메소드를 구현해 줄 클래스를 작성해야만 합니다
자바에서 인터페이스는 다음과 같은 문법을 통해 구현합니다
문법
class 클래스이름 implements 인터페이스이름 { ... }
만약 모든 추상 메소드를 구현하지 않는다면, abstract 키워드를 사용하여 추상 클래스로 선언해야 합니다