Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
Tags
- 자바자료형
- 자바형식
- 중첩for문
- 팩토리얼재귀
- 자바클래스
- 자바함수
- 자바switch문
- SQL
- 오라클
- 자바연산자
- 변수의 초기화
- 자바실행방법
- 프로그래머스
- read()함수
- 기타제어자
- 자바제어문
- 자바기타제어자
- 연산자우선순위
- 자바수우미양가
- 반복문라벨
- 자바if문
- 변수명명규칙
- 자바별찍기
- 누승재귀
- 클래스배열
- 효율적인연산
- 오버로딩
- 자바접근지정자
- 겟셋함수
- 자바상수
Archives
- Today
- Total
h-i-s-t-o-r-y
[Java] 상속(Inheritance) - 클래스 간의 관계 본문
> p.310 is-a 관계 (상속 관계)
ㄱ. 상속(inheritance) 정의 ?
기존의 클래스를 재사용하여 새로운 클래스를 작성하는 것
ㄴ. 상속의 장점
1) 적은 코딩으로 새로운 클래스를 작성할 수 있다.
2) 공통적으로 코드를 관리할 수 있기 때문에 코드 추가, 변경 용이
3) 특징 : 코드 재사용성, 코드 중복제거 -> 생산성 향상, 유지보수 용이
ㄷ. 자바에서 상속 구현 형식
class 새로작성할클래스명 extends 재사용할기존클래스 {
자손 조상
하위(sub) 상위(super)
파생(derived) 기초(base)
자식 부모
}
ㄹ. 상속계층도 : 클래스들 간의 상속관계를 그림으로 표현한 것
package days17;
public class Ex09 {
public static void main(String[] args) {
// Chapter 07 p.310 상속(inheritance)
// [ 클래스들 간의 관계 ]
// 1. has-a 관계 [==소유]
// 2. is-a 관계 (~은 ~이다.) [==상속]
Engine engine = new Engine(); //이게 좋은 코딩
//Car myCar = new Car(engine); //1.생성자 통해서 의존성 주입(DI)
Car myCar = new Car();
myCar.setEngine(engine); //2.setter 통해서 의존성 주입(DI)
myCar.speedUp(10);
myCar.stop();
System.out.println("END");
}//main
}//class
// Car - Engine 두 클래스들 간의 관계 : has-a 관계
class Car{
//필드
String name;
String gearType;
int door;
// 자료형이 클래스 타입인 엔진 필드
Engine engine;
// 어떤 방법으로 초기화할지 결정해야 하는데
// 결합력이 높아서 좋지 않은 코딩
//Engine engine = new Engine(); //1.명시적 초기화
/*
{
this.engine = new Engine(); //2.초기화 블럭
}
*/
Car(){
//this.engine = new Engine(); //3.생성자
}
public Car(Engine engine) {
this.engine = engine;
}
//getter, setter
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
void speedUp(int fuel) {
//System.out.println(this.engine); //null
this.engine.moreFuel(fuel); // java.lang.NullPointerException
}
void speedDown(int fuel) {
this.engine.lessFuel(fuel);
}
void stop() {
this.engine.stop();
}
}//class
class Engine{
int speed;
void moreFuel(int fuel) {
this.speed += fuel*0.5;
}
void lessFuel(int fuel) {
this.speed -= fuel*0.5;
}
void stop() {
this.speed = 0;
}
}//class
package days17;
public class Ex11 {
public static void main(String[] args) {
Point3D p1 = new Point3D();
// [생성자가 호출되는 순서]
// > Point 디폴트 생성자 호출됨 부모 객체 먼저 생성->자동호출(생성자)
// > Point3D 디폴트 생성자 호출됨
// END
System.out.println(p1.getX());
p1.printPoint();
System.out.println("END");
}//main
}//class
class Point{
//필드
private int x;
private int y;
//디폴트 생성자
public Point() {
System.out.println("> Point 디폴트 생성자 호출됨");
}
//x,y 생성자
public Point(int x, int y) {
this.x = x;
this.y = y;
System.out.println("> Point 2 생성자 호출됨");
}
//getter, setter
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
//method
public void printPoint() {
System.out.printf("> x=%d, y=%d\n", this.x, this.y);
}
}//class
class Point3D extends Point{
//필드
private int z;
//**생성자는 상속되지 않는다
//디폴트 생성자
public Point3D() {
System.out.println("> Point3D 디폴트 생성자 호출됨");
}
//x,y 생성자
public Point3D(int x, int y, int z) {
// 상속은 받았지만 접근지정자 private때문에 접근(참조)할 수 없어서..
// 부모의 x,y 필드는 어떻게 초기화 하는가?
// The field Point.x is not visible
// this.x = x;
// this.y = y;
// this.z = z;
System.out.println("> Point3D 3 생성자 호출됨");
}
//x,y getter,setter는 상속받았다
//getter, setter
public int getZ() {
return z;
}
public void setZ(int z) {
this.z = z;
}
//method도 상속받았다
//method
//printPoint
}//class
// z좌표 추가된 새로운 클래스
/*
class Point3D{
//필드
private int x;
private int y;
private int z;
//디폴트 생성자
public Point3D() {
System.out.println("> Point3D 디폴트 생성자 호출됨");
}
//x,y 생성자
public Point3D(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
System.out.println("> Point3D 3 생성자 호출됨");
}
//getter, setter
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getZ() {
return z;
}
public void setZ(int z) {
this.z = z;
}
//method
public void printPoint() {
System.out.printf("> x=%d, y=%d, z=%d\n", this.x, this.y, this.z);
}
}//class
*/
'Java' 카테고리의 다른 글
[Java] 기타제어자 - final / abstract (0) | 2021.07.17 |
---|---|
[Java] 오버라이딩(Overriding) / 패키지(package)와 import (0) | 2021.07.17 |
[Java] 기타제어자 - static (0) | 2021.07.17 |
[Java] 클래스, 인스턴스(변수/메서드) 용어 정리 / 변수의 초기화 (0) | 2021.07.17 |
[Java] 생성자(Constructor) / this의 의미 (0) | 2021.07.17 |
Comments