13 [인강] 자바의정석 ch07
Machine translation — switch to KO for the original.
| 상속
코드의 재사용
자손은 조상의 모든 멤버를 상속받는다 (생성자, 초기화블럭 제회)
자손의 멤버 >= 조상의 멤버
자손의 변경은 조상에 영향을 미치지 않는다
class Parent { }
class Child extends Parent { }
class Point {
int x;
int y;
}
class Point3D extends Point {
int z;
}
class SmartTv extends Tv {
boolean caption;
void displyCaption(String text) {
if (caption) {
System.out.println(text); //caption상태가 on일 때, text를 출력
}
}
}
| 클래스 간의 관계, 상속과 포함
포함: 클래스 멤버로 참조변수를 선언하는 것
class Circle {
Point c = new Point ( );
int r;
}
class Point {
int x;
int y;
}
//작은 단위 클래스를 포함해서 시간복잡도를 줄임
상속: ~은 ~이다 is
포함: ~은 ~을 가지고 있다 has
| 단일상속, Object 클래스
Java는 단일상속만 허용, C++는 다중상속 허용(충돌발생 가능성)
- 인터페이스
비중이 높은 것을 상속관계, 나머지는 포함관계로 한다
Object 클래스 //최고조상
부모가 없는 경우 object 클래스의 11개 메서드를 상속받는다(컴파일러 자동) → toString( ), equals( )....
class Circle extends Object
System.out.println(c.toString());
== System.out.println(c.);
| 오버라이딩
상속받은 조상의 메서드를 자신에 맞게 변경하는 것
class Point {
int x;
int y;
String getLocation( ) {
return " x : "+ x +," y : " + y;
}
}
class Point3D extends Point {
int z;
String getLocation( ) {
return " x : "+ x +," y : " + y + " z : " + z;
}
} //오버라이딩 구현부만 변경가능, 선언부는 같아야 함
접근 제어자를 조상 클래스의 메서드보다 좁은 범위로 변경할 수 없다
예외는 조상메서드보다 많이 선언할 수 없다
class Parent {
void parentMethod( ) throws IOException, SQLException {
}
}
class Child extends Parent {
void parentMethod( ) throws IOCException {
}
}
오버로딩: 기존에 없는 새로운 메서드 정의, 이름이 같은 new
오버라이딩: 상속받은 메서드 내용을 변경
void parentMethod ( ) { } //오버라이딩
void parentMethod ( int i ) { } //오버로딩
Comments
No comments yet. Be the first!
319 posts in 테크
- 368Supabase 프로젝트 복사하기 (Restore to a New Project)NEW
- 341Migrating from Permanent Access Tokens to Token Exchange — Why Order Matters
- 326Startup & Product Glossary: Terms Every Solo Founder Should Know
- 325Context Management — How I Do It Now
- 324Claude Code Routines vs Cowork Schedule — What's the Difference?