728x90
Section11 다형성과 타입 변환
타입 변환
자바에서는 다음과 같이 두 가지의 대표적인 타입 변환이 있습니다.
자료형(타입) 변환
클래스의 객체 타입 변환
- 클래스의 타입 변환은, 상속 관계의 클래스들 간에서만 가능합니다.
클래스 자동 타입 변환
개발자가 직접 명시해주지 않아도 자동으로 타입 변환이 일어나는 것을 클래스 자동 타입 변환이라고 부릅니다. 상속 관계에 있는 자식 클래스의 객체를 부모 타입의 객체로 변환합니다.
클래스 강제 타입 변환
자식 객체가 부모 타입으로 자동 타입 변환을 한 후, 다시 자식 타입으로 변환하는 것을 강제 타입 변환이라고 부릅니다. 개발자가 직접 명시해야만 타입 변환이 가능합니다.
다형성
다형성은 객체 지향 프로그래밍의 대표적인 특징 중 하나로, 하나의 타입으로 다양한 객체를 사용할 수 있는 것을 의미합니다.
기타
자바는 매개변수의 자료형/개수/순서를 기반으로 메서드를 구별하므로 하나의 클래스 안에서 같은 이름의 메서드를 여러 개 구현할 수 있으며, 필요에 따라 메서드를 선택해 사용할 수 있습니다.
부모 클래스에게 상속받은 메서드를 재정의하여 자식 클래스용 메서드를 구현하고 자식 객체르 통해 메서드를 호출할 땐 오버라이딩된 메서드가 호출됩니다.
728x90