일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- eclipse
- fuction
- string
- 객체형변환
- super
- constantnumber
- ALTER
- concreteclass
- object
- Vector
- value
- garbagecollection
- hamobee
- 생성자
- reference
- 추상클래스
- hashCode
- start()
- abstractclass
- Polymorphism
- Eureka
- Hashtable
- arguments
- run()
- override
- overload
- methodArea
- 콘크리트클래스
- class
- MSA
- Today
- Total
뇌운동일지
[JAVA16] 객체형변환 (overriding, overloading, 다형성, super) 본문
기본형(Primitive type) 데이터들의 형변환
객체 참조형(Object reference type) 데이터들의 형변환
서로 다른 클래스로부터의 인스턴스들의 형변환
객체형변환
Coffee c = new Americano();
왼쪽 객체변수와 오른쪽 인스턴스의 객체 유형이 서로 다른 경우,
두 클래스 간 상속 관계에 있고, Coffee가 Americano의 상위클래스인 경우에만 형변환이 일어남.
하위 클래스에서 상위 클래스로 할당하는 것은 가능 ( 상위 클래스 = 하위 클래스 )
상위 클래스에서 하위 클래스로 할당하는 경우에는 강제 형변환이 필요 ( 하위 클래스 = 상위 클래스 )
Americano는 Coffee에서 확장(extends)하는 개념이므로
상위 클래스에 하위 클래스를 할당하는 것은 문제가 없으나,
상위 클래스는 하위 클래스보다 좁은 범위의 개념이므로 강제 형변환이 필요
Coffee.java
package practiceExample;
public class Coffee {
public String name = "Coffee";
public void print() {
System.out.println("Parent method : " + name);
}
public void syrup() {
System.out.println("syrup() : No Select");
}
}
Americano.java
package practiceExample;
public class Americano extends Coffee {
public String name = "Americano";
public void print() {
System.out.println("Child method : " + name);
}
public void syrup(int pump) {
System.out.println("syrup(int pump) : " + pump + "pump");
}
public void order() {
System.out.println("Order Completed");
}
}
1. 상위 클래스 = 상위 클래스
package practiceExample;
public class Main {
public static void main(String[] args) {
Coffee c = new Coffee();
c.print();
c.syrup();
System.out.println(c.name);
}
}
상위 클래스의 객체변수에 상위 클래스의 인스턴스를 대입
상위 클래스의 멤버변수와 멤버메소드 호출
2. 하위 클래스 = 하위 클래스
package practiceExample;
public class Main {
public static void main(String[] args) {
Americano c = new Americano();
c.print();
c.syrup(); // 상위 클래스로부터 상속받음
c.syrup(1);
c.order();
System.out.println(c.name);
}
}
하위 클래스의 객체변수에 하위 클래스의 인스턴스를 대입
하위 클래스의 멤버변수와 멤버메소드 호출
상위 클래스로 부터 상속받은 메소드 호출 가능
하위 클래스 입장에서 동일한 클래스 내에 이름만 일치하는 메소드가 있으므로
method overloading
: 같은 이름의 함수에게 여러가지 일을 시키는 것
---------------> 여기까지는 일반적인 인스턴스 활용 예시
3. 상위 클래스 = 하위 클래스
package practiceExample;
public class Main {
public static void main(String[] args) {
Coffee c = new Americano();
c.print(); // override 된 하위 클래스의 메소드
c.syrup(); // 상위 클래스의 메소드
System.out.println(c.name); // 상위 클래스의 멤버변수
}
}
자식의 생성자로 상위 클래스를 만듦
상위 클래스(coffee)의 객체변수(c)에 하위 클래스(Americano)의 인스턴스 대입
상위 클래스의 멤버 변수(c.name), 상위 클래스의 멤버 메소드(c.syrup()), 하위 클래스의 오버라이드(override)된 메소드(c.print()) 호출
상위 클래스의 멤버메소드(Coffee class의 print())는 은닉화되고, 하위 클래스의 멤버메소드(Americano class의 print())가 동작
상위 클래스의 객체가 override된 하위 클래스의 메소드를 사용하고 있음 -> 다형성 ( polymorphism )
다형성을 이용할때만 부모 클래스가 자식 클래스의 멤버를 사용할 수 있음
그러나 Coffee의 객체 c는 override되지 않은 Americano의 멤버변수와 멤버메소드에 접근할 수 없다.
package practiceExample;
public class Main {
public static void main(String[] args) {
Coffee c = new Americano();
c.print();
c.syrup();
c.syrup(1); // error
c.order(); // error
System.out.println(c.name);
}
}
부모 클래스가 자식 클래스의 멤버에 접근할 수 없으므로 이는 당연한 결과이다.
하나의 인스턴스만으로 부모 클래스와 자식 클래스 모두에 접근하기 위해서 객체형변환을 한다.
4. 하위 클래스 = 상위 클래스
package practiceExample;
public class Main {
public static void main(String[] args) {
Coffee c = new Americano();
Americano a = (Americano) c; // 객체형변환
c.print();
c.syrup();
System.out.println(c.name);
a.print();
a.syrup();
a.syrup(3);
a.order();
System.out.println(a.name);
}
}
3의 코드에 추가
상위클래스의 객체변수(c)에는 하위클래스의 인스턴스가 대입되어 있음
하위클래스의 객체변수(a)에 상위클래스의 인스턴스(c) 대입
Coffee의 instance c를 Americano 형으로 변환하여, Americano 형 객체변수 a에 대입 -> 객체형변환
객체형변환 이전에는 접근할 수 없었던 Americano의 메소드들에 접근할 수 있게 된다.
여기서 은닉화된 Coffee의 name과 print()에도 접근할 수 있는 방법이 있다.
그렇게 하기 위해서는 Americano class 의 수정이 필요하다.
Americano.java - 수정
package practiceExample;
public class Americano extends Coffee {
public String name = "Americano";
public void print() {
System.out.println("Child method : " + name);
}
public void syrup(int pump) {
System.out.println("syrup(int pump) : " + pump + "pump");
}
public void order() {
System.out.println("Order Completed");
}
public void coffeeName() {
System.out.println(super.name);
}
public void type() {
super.print();
}
}
coffeeName()과 type()에서 super keyword를 활용하여 Coffee의 print()를 호출해주었다.
main method
package practiceExample;
public class Main {
public static void main(String[] args) {
Coffee c = new Americano();
Americano a = (Americano) c; // 객체형변환
a.print();
a.syrup();
a.syrup(3);
a.order();
System.out.println(a.name);
a.type();
a.coffeeName();
}
}
main method에서 type()과 coffeeName()을 호출하여 은닉화되어있던 Coffee의 멤버들에 접근할 수 있다.
다형성으로 사용하다가 하위 member가 필요해지면 객체형변환을 한다
'JAVA' 카테고리의 다른 글
[JAVA18] 추상 클래스 (Abstract class) VS 인터페이스(Interface) (0) | 2020.03.23 |
---|---|
[JAVA17] 상속 관계에서의 생성자, this, super (0) | 2020.03.20 |
[JAVA15] overridding VS overloading (0) | 2020.03.20 |
[JAVA14] 접근제한자(Access Modifier), 상속(inheritance) (0) | 2020.03.19 |
[JAVA13] String (0) | 2020.03.18 |