Polymorphism
What is Polymorphism? #
- It means that a single
objectcan have multiple types. - In Java, this polymorphism is implemented by allowing a reference variable of a parent class type to refer to an instance of a child class type.
Polymorphism of Reference Variables #
In Java, for polymorphism, a reference variable of a parent class type can refer to an instance of a child class type. In this case, the number of members that the reference variable can use must be less than or equal to the number of members of the actual instance for it to be able to refer to it.
class Parent{...}
class Child extends Parent{...}
...
Parent pa = new Parent(); // 허용
Child ch = new Child(); // 허용
Parent pc = new Child(); // 허용
Child cp = new Parent(); // 오류 발생 !
- Naturally, a reference variable of a specific type can refer to an instance of the same type (because the number of members that the reference variable can use is the same as the number of members of the actual instance).
- A reference variable of a parent class type can also refer to an instance of a child class type (because the number of members that the reference variable can use is less than the number of members of the actual instance).
- However, in the opposite case, a reference variable of a child class type cannot refer to an instance of a parent class type (because the number of members that the reference variable can use is greater than the number of members of the actual instance).
Type Conversion of Reference Variables #
In Java, reference variables can also undergo type conversion under the following conditions:
- Type conversion can only occur between classes that are in an inheritance relationship.
- Type conversion from a child class type to a parent class type can be omitted.
- However, type conversion from a parent class type to a child class type must be explicitly specified.
Similar to type conversion of primitive types, type conversion of reference variables also uses the type cast operator
().
(변환할타입의 클래스 이름) 변환할 참조 변수
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; // 직접적인 상속 관계가 아니므로, 오류 발생.
The instanceof Operator #
- Due to this polymorphism, there arises a need to check the actual type of the instance that a reference variable is referring to at runtime.
- Java provides the
instanceofoperator, which allows checking the actual type of the instance that a reference variable is referring to.
참조변수 instanceof 클래스이름
If the type of the instance that the reference variable on the left actually refers to is the class type passed on the right, it returns true; otherwise, it returns false.
If the reference variable points to null, it returns false.
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
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
}
}