Day 29 (23.02.06)

Jane·2023년 2월 6일
0

IT 수업 정리

목록 보기
31/124

1. 상속에서 주의해야할 점

1-1. 생성자 함수는 맨 앞에 써야한다

  • 메모리는 부모 ~ 자식 순으로 올라간다.
  • 부모 클래스를 먼저 호출하고, 자식 클래스를 나중에 호출한다.
public ColorTV(int size, int color) {
	super(size);
	this.color = color;
}

1-2. Java는 단일 상속만 가능하다

  • Java는 다중상속을 지원하지 않는다. (C++은 가능하다)
    클래스 안에 있는 변수의 중복 문제, 변수에 접근하는 방법의 문제...
    개발자 입장에서는 복잡해질 수 있으므로 다중상속을 지원하지 않음

  • 여러 클래스가 연속적으로 단일 상속을 받는 것은 가능하다.
import java.util.Scanner;

class A {
	A(){
		System.out.println("A class");
	}
}
class B extends A{
	B(){
		System.out.println("B class");
	}
}
class C extends B{
	C(){
		System.out.println("C class");
	}
}

public class JavaTest {
	public static void main(String[] args) {
		new C();
	}

}

[Console]
A class
B class
C class

2. 상속의 관계

2-1. 하위 클래스의 특성

  • 하위 클래스 = 상위 클래스 + 하위 클래스의 특성
    (class 스마트폰 extends 모바일폰)

2-2. 클래스의 관계성

  • IS-A 관계 : 상속으로 표현한다.
    (스마트폰은 모바일폰이다. 사람은 동물이다.)
class 동물 {}

class 사람 extends 동물 {}
classextends 동물 {}
classextends 동물 {}
  • HAS-A 관계 : 클래스 안의 데이터 멤버로 표현한다.
    IS-A 관계가 확실하지 않으면 HAS-A로 표현한다.
    (컴퓨터는 CPU, 메모리를 갖고 있다.)
class Computer{
	Cpu cpu;
	Memory memory;
}

class MobilePhone {
	protected String number; // 전화번호

	public MobilePhone(String num) {
		number = num;
	}

	public void answer() {
		System.out.println("Hi~ from " + number);
	}
}

class SmartPhone extends MobilePhone {
	private String androidVer; // 운영체제 버전

	public SmartPhone(String num, String ver) {
		super(num);
		androidVer = ver;
	}

	public void playApp() {
		System.out.println("App is running in " + androidVer);
	}
}

public class JavaTest {
	public static void main(String[] args) {
		SmartPhone phone = new SmartPhone("010-555-7777", "Nougat");

		phone.answer();
		phone.playApp();
	}

}

[Console]
Hi~ from 010-555-7777
App is running in Nougat

3. 부모 - 자식 클래스 간의 참조 (다형성)

부모는 자식이 될 수 있지만, 자식은 부모가 될 수 없다

3-1. 객체 생성

  • 부모 데이터 타입으로 자식 객체를 만들 수 있으나,
    자식 데이터 타입으로 부모 객체를 만들 수 없다.
    (형 변환 시 해당하는 클래스의 변수와 함수가 없는 부분이 있으므로 컴파일 에러)

class A {
	int a;

	void printA() {
		System.out.println("A");
	}
}

class B extends A{
	int b;

	void printB() {
		System.out.println("B");
	}
}

public class JavaPractice {

	public static void main(String[] args) {
		A a = new A();
		a.printA();
		
		B b = new B();
		b.printB();
		b.printA();
		
		A aa = new B();
		aa.printA();
		
		// B bb = new A();
	}

}

3-2. 함수 접근

  • 자식 클래스는 부모 클래스에 접근할 수 있으나,
    부모 클래스는 자식 클래스에 접근할 수 없다.
    (부모 클래스에 자식 클래스의 변수와 함수가 없기 때문에 컴파일 에러)

class MobilePhone {
	protected String number; // 전화번호

	public MobilePhone(String num) {
		number = num;
	}

	public void answer() {
		System.out.println("Hi~ from " + number);
	}
}

class SmartPhone extends MobilePhone {
	private String androidVer; // 운영체제 버전

	public SmartPhone(String num, String ver) {
		super(num);
		androidVer = ver;
	}

	public void playApp() {
		System.out.println("App is running in " + androidVer);
	}
}

public class JavaTest {
	public static void main(String[] args) {
		SmartPhone phone = new SmartPhone("010-555-7777", "Nougat");
		MobilePhone phone2 = new SmartPhone("010-999-3333", "Nougat");

		phone.answer();
		phone.playApp();
		System.out.println();
		phone2.answer();
		//phone2.playApp();
	}

}
  • SmartPhone 클래스에서는 answer(), playApp() 모두 접근 가능,
    MobilePhone 클래스는 playApp()이 없으므로 컴파일 에러!

4. 예제 : Employee와 Regular의 관계

class Employee {
	String name;
	int age;
	String address;
	String position;
	int salary;

	public Employee(String name, int age, String address, String position) {
		this.name = name;
		this.age = age;
		this.address = address;
		this.position = position;
	}

	public void printInfo() {
		System.out.println("이름 : " + name);
		System.out.println("나이 : " + age);
		System.out.println("주소 : " + address);
		System.out.println("부서 : " + position);
	}

}

class Regular extends Employee {

	public Regular(String name, int age, String address, String position) {
		super(name, age, address, position);
	}

	public void setSalary(int salary) {
		super.salary = salary;
	}

	public void printInfo() {
		super.printInfo();
		System.out.println("정규직입니다");
		System.out.println("월급 : " + super.salary);
	}
}

public class JavaPractice {

	public static void main(String[] args) {
		Regular a = new Regular("김철수", 20, "인천", "대리");
		a.setSalary(2000);
		a.printInfo();
	}

}

[Console]
이름 : 김철수
나이 : 20
주소 : 인천
부서 : 대리
정규직입니다
월급 : 2000

profile
velog, GitHub, Notion 등에 작업물을 정리하고 있습니다.

0개의 댓글