https://journeyjeong.tistory.com/15
[JAVA] Inheritance : 상속 _01
상속 (inheritane) - 부모클래스: 상위클래스, 슈퍼클래스 - 자식클래스: 하위클래스, 서브클래스 01. 개념 : 부모의 멤버를 상속받아 자식이 쓸수 있게 해준다. 02. 목적 : 코드의 중복, 효율적으로 개
journeyjeong.tistory.com
Inheritance: 상속_ 02
01.
슈퍼클래스와 서브클래스의 변수이름이 동일할때,
서브클래스에서 동일한 변수의 이름으로 출력 할때는 서브클래스 값이 기본으로 출력됨.
그렇기에 슈퍼클래스의 값을 가지고 올때는 서브클래스에서 super.x 로 호출해야함
예제 01.
package fields;
public class AAA {
int a = 10;
int b = 20;
public AAA() {
System.out.println("AAA");
}
}
package fields;
public class BBB extends AAA{
int b = 200;
int c = 300;
void disp() {
System.out.println(this.a);
System.out.println(this.b);
System.out.println(this.c);
System.out.println("--------");
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println("--------");
System.out.println(super.b);
System.out.println(this.b);
}
public BBB() {
System.out.println("BBB");
} // 출력: AAA BBB
}
Main.
package fields;
public class ExtendTest04 {
public static void main(String[] args) {
BBB bbb = new BBB();
System.out.println(bbb.toString());
System.out.println(bbb.c);
System.out.println(bbb.a);
System.out.println(bbb.b);
// System.out.println(super.b); // 안됨
bbb.disp();
}
}
출력>
AAA
BBB
fields.BBB@5e91993f
300
10
200
10
200
300
--------
10
200
300
--------
20
200
예제 02. 오버로딩 메서드 호출
package method;
public class CCC {
void cc() {
System.out.println("CCC-cc() 메서드입니다. ");
}
void display() {
System.out.println("CCC-display 메서드 입니다.");
}
}
package method;
public class DDD extends CCC {
void dd() {
System.out.println("DDD-dd() 메서드입니다. ");
}
void display() {
super.display();
System.out.println("DDD-display 메서드 입니다.");
}
}
Main.
package method;
public class ExtendTest05 {
public static void main(String[] args) {
DDD d= new DDD();
d.dd();
d.cc();
d.display();
}
}
> 출력
DDD-dd() 메서드입니다.
CCC-cc() 메서드입니다.
CCC-display 메서드 입니다.
DDD-display 메서드 입니다.
02. 다형성
: 다형성이란 하나의 메소드나 클래스가 있을 때 이것들이 다양한 방법으로 동작하는 것을 의미
: 다형성은 객체나 인터페이스 또는 추상과 같이 철학적인 느낌을 자아내는 용어
: 일반적으로 부모클래스의 참조변수로 자식 클래스 개체(자식클래스로 인스턴스화 한)를 참조하는데 사용될때 발생합니다.
예제 03.
package fields;
public class AAA {
int a = 10;
int b = 20;
public AAA() {
System.out.println("AAA");
}
}
package fields;
public class BBB extends AAA{
int b = 200;
int c = 300;
void disp() {
System.out.println(this.a);
System.out.println(this.b);
System.out.println(this.c);
System.out.println("--------");
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println("--------");
System.out.println(super.b);
System.out.println(this.b);
}
public BBB() {
System.out.println("BBB");
} // 출력: AAA BBB
}
Main.
package fields;
public class ExtendTest06 {
public static void main(String[] args) {
// OOP 에서 다형성의 가장 일반적인 사용은
// AAA 부모 클래스 참조변수로
// BBB 자식 클래스 개체(자식클래스로 인스턴스화한)를 참조하는데 사용될때 발생합니다.
AAA ab= new BBB(); // AAA공간에만 접근할게요, BBB 공간에는 접근하지 못함
System.out.println(ab.a);
System.out.println(ab.b);
}
}
출력>
AAA
BBB
10
20
'JAVA _기초' 카테고리의 다른 글
[JAVA] 콜렉션(Collection) (0) | 2023.02.27 |
---|---|
[JAVA] Inheritance : 상속_03 (0) | 2023.02.23 |
[JAVA] Inheritance : 상속 _01 (0) | 2023.02.21 |
[JAVA] Exceptions: 예외 (0) | 2023.02.20 |
[JAVA] 자바의 Enum, 열거형 이란 (0) | 2023.02.17 |