[Java] Inheritance(์ƒ์†)๐Ÿ‘ช

soyeonยท2022๋…„ 7์›” 12์ผ
0
post-thumbnail

์ƒ์†

: ๋ถ€๋ชจ class๊ฐ€ ๊ฐ€์ง€๋Š” ๋‚ด์šฉ์„ ํ™•์žฅํ•ด์„œ ์ž์‹ class๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ์‹

ex) ํ•™์‚ฌ ์‹œ์Šคํ…œ
Class - Teacher, Student, Staff => ๊ณตํ†ต์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” field, method๊ฐ€ ์กด์žฌํ•œ๋‹ค.
์ฝ”๋“œ์— ์ค‘๋ณต์ด ๋ฐœ์ƒํ•œ๋‹ค. ์—ฌ๊ธฐ์—์„œ ๊ณตํ†ต์œผ๋กœ ์ถ”๊ฐ€ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด ์ƒ๊ธฐ๋ฉด ๊ฐ์ž ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.

class Student {
	
	String name;  // ์ด๋ฆ„
	String mobile;  // ์ „ํ™”๋ฒˆํ˜ธ
	String dept;  // ํ•™๊ณผ
	
}

class Teacher {
	
	String name;  // ์ด๋ฆ„
	String mobile;  // ์ „ํ™”๋ฒˆํ˜ธ
	String subject;  // ๊ณผ๋ชฉ
}

class Staff {
	
	String name;  // ์ด๋ฆ„
	String mobile;  // ์ „ํ™”๋ฒˆํ˜ธ
	int salary;  // ์›”๊ธ‰
	
}

์ƒ์†์œผ๋กœ ํ•ด๊ฒฐํ•œ๋‹ค.

class Person {
	String name;  // ์ด๋ฆ„
	String mobile;  // ์ „ํ™”๋ฒˆํ˜ธ
}

// tightly coupled
class Student extends Person{
	String dept;  // ํ•™๊ณผ
	
}

class Teacher extends Person{	
	String subject;  // ๊ณผ๋ชฉ
}

class Staff extends Person{	
	int salary;  // ์›”๊ธ‰	
}

Java ์ƒ์†์˜ ํŠน์ง•

  • extends keyword๋กœ ์ƒ์†์„ ๊ตฌํ˜„ํ•œ๋‹ค.

  • Java๋Š” ๋‹จ์ผ ์ƒ์†๋งŒ ์ง€์›ํ•œ๋‹ค. ๋‹ค์ค‘ ์ƒ์†์€ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค.
    : extends ๋’ค์— ์—ฌ๋Ÿฌ class๊ฐ€ ์˜ฌ ์ˆ˜ ์—†๋‹ค.

    class Human {
		String name;
    }

    class Person {
        String name;  // ์ด๋ฆ„
        String mobile;  // ์ „ํ™”๋ฒˆํ˜ธ
    }

    class Student extends Person, Human{  // ์˜ค๋ฅ˜ ๋ฐœ์ƒ
        String dept;  // ํ•™๊ณผ

    }
  • class๊ฐ€ "tightly coupled" -> ์žฌ์‚ฌ์šฉ์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

์ƒ์†์ด ๋˜์ง€ ์•Š๋Š” ๊ฒƒ

: constructor๋Š” ์ƒ์†๋˜์ง€ ์•Š๋Š”๋‹ค.
: private access modifier๋กœ ์ง€์ •๋œ field, method๋„ ์ƒ์†๋˜์ง€ ์•Š๋Š”๋‹ค.

์ƒ์†์—์„œ Instance ์ƒ์„ฑ

instance๋Š” ๋ฌด์กฐ๊ฑด Heap์— ์ƒ์„ฑ๋œ๋‹ค.

class Person {
	String name;  // ์ด๋ฆ„
	String mobile;  // ์ „ํ™”๋ฒˆํ˜ธ
}

class Student extends Person {
	String dept;
}

public class Main {
	public static void main(String[] args) {
		Student s = new Student();
        
        // is-a relationship
		// subclass is a superclass
        
        // Person s = new Student();
		// subclass ํƒ€์ž…์„ ์จ์•ผ ํ•  ๊ณณ์— superclass ํƒ€์ž…์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค.
        // ์ด๋•Œ๋Š” s๊ฐ€ Student๋ฅผ ๊ฐ€๋ฆฌํ‚ค์ง€ ์•Š๊ณ , ์•ˆ์˜ Person์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
        
        // Object s = new Student();
        // ์ด๋•Œ๋Š” Person ์•ˆ์˜ Object๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
        
        // => Polymorphism(๋‹คํ˜•์„ฑ)
	}
}

์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด Person์„ ์ƒ์† ๋ฐ›์•„์„œ name, mobile, dept๊ฐ€ Student ๊ฐ์ฒด์— ๋‹ด๊ฒจ์„œ ์ƒ๊ธธ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ ์•„๋‹ˆ๋‹ค.

=> ์‹ค์ œ ์ƒ์†์—์„œ Instance ์ƒ์„ฑ ๋ฐฉ๋ฒ•
: ์ƒ์œ„ ๊ฐ์ฒด๋ฅผ ๋จผ์ € ๋งŒ๋“ค๊ณ , ๊ทธ๋ฅผ ๊ฐ์‹ธ๋Š” ํ•˜์œ„ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ๋งŒ๋“ ๋‹ค.

  1. class๊ฐ€ ์ƒ์†์„ ๋ฐ›๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ๋”์ด์ƒ ์ƒ์†์„ ๋ฐ›๋Š” class๊ฐ€ ์—†์„ ๋•Œ ๊นŒ์ง€(Object๋ฅผ ์ƒ์†๋ฐ›๋Š” class๊ฐ€ ๋‚˜์˜ฌ ๋•Œ ๊นŒ์ง€) ์ƒ์œ„ class๋กœ ์˜ฌ๋ผ๊ฐ„๋‹ค.
  2. ์ตœ์ƒ์œ„ ํด๋ž˜์Šค์˜ constructor๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. -> Object constructor๊ฐ€ ๋งŒ๋“ค์–ด์ง„๋‹ค.
  3. Object ๋ฐ‘์˜ Person ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค.
    : name, mobile ๊ณต๊ฐ„์ด ๋งŒ๋“ค์–ด์ง„๋‹ค.
  4. Person ๋ฐ‘์˜ Student ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค.
    : dept ๊ณต๊ฐ„์ด ๋งŒ๋“ค์–ด์ง„๋‹ค.
  5. Stack์˜ s ๋ณ€์ˆ˜๊ฐ€ ํ•ด๋‹น instance๋ฅผ ํฌ์ธํŒ… ํ•˜๊ฒŒ ๋œ๋‹ค.

Java์˜ ๋ชจ๋“  class๋Š” ๋ฌด์กฐ๊ฑด Object class๋ฅผ ์ƒ์†ํ•˜๊ณ  ์žˆ๋‹ค.
Java์˜ ๋ชจ๋“  class๋Š” ํŠน์ • package ์•ˆ์— ํฌํ•จ๋˜์–ด ์žˆ๋‹ค.
-> Object class๋Š” Java.lang.Object

method overriding (๋ฉ”์†Œ๋“œ ์žฌ์ •์˜)

: ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์ƒ์œ„ ํด๋ž˜์Šค์— ์žˆ๋Š” ๊ฒƒ์„ ์žฌ์ •์˜ ํ•œ๋‹ค.

class Person {
	String name;  // ์ด๋ฆ„
	String mobile;  // ์ „ํ™”๋ฒˆํ˜ธ
	
	public void printAll() {
		System.out.println("๋ชจ๋‘ ์ถœ๋ ฅ!");
	}
}

class Student extends Person {
	String name;  // ์ผ๋ฐ˜์ ์ด์ง€ ์•Š๋‹ค.
	String dept;
	
	public void printAll() {
		System.out.println("์˜ค๋ฒ„๋ผ์ด๋”ฉ!");
	}
}

public class Main {
	public static void main(String[] args) {
		Student s = new Student();
	}
}

this

: ํ˜„์žฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด์˜ reference

this()

: ์ž์‹ ์˜ class๊ฐ€ ๊ฐ€์ง„ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

class Person {
	String name;  // ์ด๋ฆ„
	String mobile;  // ์ „ํ™”๋ฒˆํ˜ธ
	
	public void printAll() {
		System.out.println("๋ชจ๋‘ ์ถœ๋ ฅ!");
	}
}

class Student extends Person {
	String name; // ์ผ๋ฐ˜์ ์ด์ง€ ์•Š๋‹ค.
	String dept;
	
	// default constructor
	public Student() {
		this("ํ™๊ธธ๋™");
	}
	
	public Student(String name) {
		this.name = name;		
	}
	
	public void printAll() {
		System.out.println("์˜ค๋ฒ„๋ผ์ด๋”ฉ!");
	}
}

public class Main {
	public static void main(String[] args) {
		Student s = new Student();
	}
}

super

: ํ˜„์žฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด์˜ reference์ธ๋ฐ type์ด ์ƒ์œ„ type์ธ ๊ฒƒ

super()

: ์ƒ์œ„ class์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

์ƒ์† ๋ฌธ์ œ

  • ๋‹ค์Œ ์ฝ”๋“œ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š”?
package lecture0712;

class SuperClass {
	
	// static method
	static int staticCall(String msg) {
		System.out.println(msg);
		return 100;
	}
	
	// fields
	int a = staticCall("1๋ฒˆ์ž…๋‹ˆ๋‹ค.");
	static int b = staticCall("2๋ฒˆ์ž…๋‹ˆ๋‹ค.");
	
	// constructor
	public SuperClass() {
		staticCall("3๋ฒˆ์ž…๋‹ˆ๋‹ค.");
	}
	
	public SuperClass(int i) {
		this();
		staticCall("4๋ฒˆ์ž…๋‹ˆ๋‹ค.");
	}
	
	// method
	public void myFunc() {
		System.out.println("5๋ฒˆ์ž…๋‹ˆ๋‹ค.");
	}
}

public class InheritanceTest extends SuperClass {
	
	// fields
	int c = staticCall("6๋ฒˆ์ž…๋‹ˆ๋‹ค.");
	static int d = staticCall("7๋ฒˆ์ž…๋‹ˆ๋‹ค.");
	
	// constructor
	public InheritanceTest() {
		super(100);
		staticCall("8๋ฒˆ์ž…๋‹ˆ๋‹ค.");
		super.myFunc();
	}
	
	@Override
	public void myFunc() {
		System.out.println("9๋ฒˆ์ž…๋‹ˆ๋‹ค.");
	}
	
	public static void main(String[] args) {
		System.out.println("10๋ฒˆ์ž…๋‹ˆ๋‹ค.");
		SuperClass obj = new InheritanceTest();
		obj.myFunc();  // => ๋™์  ๋ฐ”์ธ๋”ฉ(dynamic binding)
	}
}

๋‚ด ํ’€์ด : 2-7-10-1-6-3-4-8-5-9
์ •๋‹ต : 2-7-10-1-3-4-6-8-5-9

  1. Method Area์— SuperClass์˜ ์ •๋ณด๊ฐ€ ์˜ฌ๋ผ๊ฐ„๋‹ค. static์ธ b์˜ ๊ณต๊ฐ„์ด ์ƒ๊ธด๋‹ค.
    => 2๋ฒˆ ์ถœ๋ ฅ
  2. Method Area์— InheritanceTest์˜ ์ •๋ณด๊ฐ€ ์˜ฌ๋ผ๊ฐ„๋‹ค. static์ธ d์˜ ๊ณต๊ฐ„์ด ์ƒ๊ธด๋‹ค.
    => 7๋ฒˆ ์ถœ๋ ฅ
  3. main method ์‹œ์ž‘
    => 10๋ฒˆ ์ถœ๋ ฅ
  4. new๋กœ instance๋ฅผ ๋งŒ๋“ ๋‹ค. InheritnaceTest()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
  5. ์ƒ์œ„ class์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๋ฉด field๊ฐ€ ์ดˆ๊ธฐํ™”๊ฐ€ ๋จผ์ € ๋˜์–ด์•ผ ํ•œ๋‹ค.
    => 1๋ฒˆ ์ถœ๋ ฅ
  6. super(100)์ด ํ˜ธ์ถœ๋œ๋‹ค. this()๋กœ SuperClass()๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.
    => 3๋ฒˆ ์ถœ๋ ฅ
    => 4๋ฒˆ ์ถœ๋ ฅ
  7. InheritanceClass์˜ field๊ฐ€ ์ดˆ๊ธฐํ™”๋œ๋‹ค.
    => 6๋ฒˆ ์ถœ๋ ฅ
  8. InheritanceTest()์˜ ๋‹ค์Œ์ค„ ์‹คํ–‰
    => 8๋ฒˆ ์ถœ๋ ฅ
  9. super.myFunc() ์‹คํ–‰
    => 5๋ฒˆ ์ถœ๋ ฅ
  10. obj.myFunc() ์‹คํ–‰
    => 9๋ฒˆ ์ถœ๋ ฅ

is-a relationship
: class๋กœ๋ถ€ํ„ฐ ํŒŒ์ƒ๋œ instance์˜ data type์„ ์ƒ์œ„ type์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
=> ๋‹คํ˜•์„ฑ(Polymorphism)

๋™์  ๋ฐ”์ธ๋”ฉ(dynamic binding)
: ๊ฐ์ฒด์— ๋Œ€ํ•œ type์ด ์ƒ์œ„ type์ด๋ผ ํ• ์ง€๋ผ๋„ ๋งŒ์•ฝ overriding๋œ method๊ฐ€ ํ•˜์œ„์— ์กด์žฌํ•œ๋‹ค๋ฉด method๋Š” overriding ๋œ method๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€