λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Java

[Java-기초] μΈν„°νŽ˜μ΄μŠ€ 상속

by μ½”λ”©ν•˜λŠ” λΆ•μ–΄ 2021. 2. 21.
λ°˜μ‘ν˜•

β–Ά μΈν„°νŽ˜μ΄μŠ€ μƒμ†ν•˜κΈ°

μΈν„°νŽ˜μ΄μŠ€ κ°„ 상속은 κ΅¬ν˜„ μ½”λ“œλ₯Ό 톡해 κΈ°λŠ₯을 μƒμ†ν•˜λŠ” 것이 μ•„λ‹ˆλ―€λ‘œ ν˜• 상속이라고 λΆ€λ₯Έλ‹€.

클래슀의 κ²½μš°μ—λŠ” ν•˜λ‚˜μ˜ 클래슀만 상속받을 수 μžˆμ§€λ§Œ, μΈν„°νŽ˜μ΄μŠ€λŠ” μ—¬λŸ¬ 개λ₯Ό λ™μ‹œμ— 상속받을 수 μžˆλ‹€.

ν•œ μΈν„°νŽ˜μ΄μŠ€κ°€ μ—¬λŸ¬ μΈν„°νŽ˜μ΄μŠ€λ₯Ό μƒμ†λ°›μœΌλ©΄, 상속받은 μΈν„°νŽ˜μ΄μŠ€λŠ” μƒμœ„ μΈν„°νŽ˜μ΄μŠ€μ— μ„ μ–Έν•œ 좔상 λ©”μ„œλ“œλ₯Ό λͺ¨λ‘ κ°€μ§€κ²Œ λœλ‹€.

​

MyInterface μΈν„°νŽ˜μ΄μŠ€λŠ” X와 Y μΈν„°νŽ˜μ΄μŠ€λ₯Ό 상속받고, MyClass ν΄λž˜μŠ€λŠ” MyInterface μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‹€μ œλ‘œ μ‚¬μš©ν•  수 μžˆλ„λ‘ κ΅¬ν˜„ν•œλ‹€. MyInterface μΈν„°νŽ˜μ΄μŠ€λŠ” 두 μΈν„°νŽ˜μ΄μŠ€λ₯Ό 상속받고 μžμ‹ μ΄ 좔상 λ©”μ„œλ“œλ₯Ό 1개 κ°€μ§€κ³  μžˆμœΌλ―€λ‘œ 상속받은 ν›„ 좔상 λ©”μ„œλ“œλ₯Ό 총 3개 κ°€μ§€κ²Œ λœλ‹€. λ”°λΌμ„œ MyClassκ°€ κ΅¬ν˜„ν•΄μ•Ό ν•  좔상 λ©”μ„œλ“œ κ°œμˆ˜λŠ” 총 3κ°œλ‹€.

​

X와 Y μΈν„°νŽ˜μ΄μŠ€

 

X와 Yλ₯Ό 상속받은 MyInterface μΈν„°νŽ˜μ΄μŠ€

 

​

package interfaceex;

public class MyClass implements MyInterface {
	@Override
	public void x() {  // X μΈν„°νŽ˜μ΄μŠ€μ—μ„œ 상속받은 x() λ©”μ„œλ“œ κ΅¬ν˜„
		System.out.println("x()");
	}

	@Override
	public void y() {  // Y μΈν„°νŽ˜μ΄μŠ€μ—μ„œ 상속받은 y() λ©”μ„œλ“œ κ΅¬ν˜„
		System.out.println("y()");
	}

	@Override
	public void myMethod() {  // MyInterface μΈν„°νŽ˜μ΄μŠ€μ˜ myMethod() λ©”μ„œλ“œ κ΅¬ν˜„
		System.out.println("myMethod()");
	}
    
}

 

package interfaceex;

public class MyClassTest {
	public static void main(String[] args) {
		MyClass mClass = new MyClass();
		X xClass = mClass;  // μƒμœ„ μΈν„°νŽ˜μ΄μŠ€ Xν˜•μœΌλ‘œ λŒ€μž…ν•˜λ©΄ 
		xClass.x();  // X에 μ„ μ–Έν•œ λ©”μ„œλ“œλ§Œ 호좜 κ°€λŠ₯

		Y yClass = mClass;  // μƒμœ„ μΈν„°νŽ˜μ΄μŠ€ Yν˜•μœΌλ‘œ λŒ€μž…ν•˜λ©΄
		yClass.y();  // Y에 μ„ μ–Έν•œ λ©”μ„œλ“œλ§Œ 호좜 κ°€λŠ₯

		MyInterface iClass = mClass;  // κ΅¬ν˜„ν•œ μΈν„°νŽ˜μ΄μŠ€ν˜• λ³€μˆ˜μ— λŒ€μž…ν•˜λ©΄ μΈν„°νŽ˜μ΄μŠ€κ°€ μƒμ†ν•œ λͺ¨λ“  λ©”μ„œλ“œ 호좜 κ°€λŠ₯
		iClass.myMethod();
		iClass.x();
		iClass.y();
	}
    
}

<μ‹€ν–‰ κ²°κ³Ό>

​

 

 

β–Ά μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„κ³Ό 클래슀 상속 ν•¨κ»˜ μ“°κΈ°

ν•œ ν΄λž˜μŠ€μ—μ„œ 클래슀 상속과 μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„μ„ λͺ¨λ‘ ν•  μˆ˜λ„ μžˆλ‹€.

​​

Shelf.java

package bookshelf;

import java.util.ArrayList;

public class Shelf {
	protected ArrayList<String> shelf;  // 자료λ₯Ό μˆœμ„œλŒ€λ‘œ μ €μž₯ν•  ArrayList μ„ μ–Έ

	public Shelf() {  // λ””ν΄νŠΈ μƒμ„±μžλ‘œ Shelf 클래슀λ₯Ό μƒμ„±ν•˜λ©΄ ArrayList도 생성됨
		shelf = new ArrayList<String>();
	}

	public ArrayList<String> getShelf() {
		return shelf;
	}

	public int getCount() {
		return shelf.size();
	}
    
}

-6ν–‰μ—μ„œ Shelf ν΄λž˜μŠ€μ— 자료λ₯Ό μˆœμ„œλŒ€λ‘œ μ €μž₯ν•  λ°°μ—΄ 객체λ₯Ό μ„ μ–Έν–ˆλ‹€.

​

​

Queue.java

package bookshelf;

public interface Queue {
	void enQueue(String title);   // λ°°μ—΄μ˜ 맨 λ§ˆμ§€λ§‰μ— μΆ”κ°€
	String deQueue();             // λ°°μ—΄μ˜ 맨 처음 ν•­λͺ© λ°˜ν™˜
	int getSize();                // ν˜„μž¬ Queue에 μžˆλŠ” 개수 λ°˜ν™˜
}

-4ν–‰μ˜ euQueue( ) λ©”μ„œλ“œλŠ” μž…λ ₯λ˜λŠ” μš”μ†Œ 값을 λ°°μ—΄μ˜ 맨 뒀에 μΆ”κ°€ν•œλ‹€.

-5ν–‰μ˜ deQueue( ) λ©”μ„œλ“œλŠ” 맨 μ•žμ— μžˆλŠ” μš”μ†Œλ₯Ό μ œκ±°ν•˜κ³  κ·Έ 값을 λ°˜ν™˜ν•œλ‹€.

​

​

BookShelf.java

package bookshelf;

public class BookShelf extends Shelf implements Queue {
	@Override
	public void enQueue(String title) {  // 배열에 μš”μ†Œ μΆ”κ°€
		shelf.add(title);
	}

	@Override
	public String deQueue() {  // 맨 처음 μš”μ†Œλ₯Ό λ°°μ—΄μ—μ„œ μ‚­μ œν•˜κ³  λ°˜ν™˜
		return shelf.remove(0);
	}

	@Override
	public int getSize() {  // λ°°μ—΄ μš”μˆ˜ 개수 λ°˜ν™˜
		return getCount();
	}
    
}

-3행을 보면 BookShelf ν΄λž˜μŠ€λŠ” Shelf 클래슀λ₯Ό 상속받고 Queue μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•œλ‹€. Shelf ν΄λž˜μŠ€κ°€ κ°€μ§€κ³  μžˆλŠ” ArrayList 배열을 μ‚¬μš©ν•˜μ—¬ Queue μΈν„°νŽ˜μ΄μŠ€μ—μ„œ μ„ μ–Έν•œ λ©”μ„œλ“œλ₯Ό λͺ¨λ‘ κ΅¬ν˜„ν•œλ‹€.

​

​

BookShelfTest.java

package bookshelf;

public class BookShelfTest {
	public static void main(String[] args) {
		Queue shelfQueue = new BookShelf();
		shelfQueue.enQueue("νƒœλ°±μ‚°λ§₯ 1");
		shelfQueue.enQueue("νƒœλ°±μ‚°λ§₯ 2");
		shelfQueue.enQueue("νƒœλ°±μ‚°λ§₯ 3");

		System.out.println(shelfQueue.deQueue());
		System.out.println(shelfQueue.deQueue());
		System.out.println(shelfQueue.deQueue());
	}
    
}

<μ‹€ν–‰ κ²°κ³Ό>

​

 

 

μ—°μŠ΅λ¬Έμ œ Q7)

숫자 μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜μ—λŠ” μ—¬λŸ¬ 정책이 μ‘΄μž¬ν•œλ‹€. λ‹€μŒ μ‹œλ‚˜λ¦¬μ˜€μ²˜λŸΌ μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ„€κ³„ν•˜κ³  μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•  μ•Œκ³ λ¦¬μ¦˜ 클래슀λ₯Ό λ§Œλ“€μ–΄ 보라.

​

<쑰건>

package sorting;

import java.io.IOException;

public class SortTest {
	public static void main(String[] args) throws IOException {
			System.out.println("μ •λ ¬ 방식을 μ„ νƒν•˜μ„Έμš”.");
			System.out.println("B : BubbleSort ");
			System.out.println("H : HeapSort ");
			System.out.println("Q : QuickSort ");

			int ch = System.in.read();
			Sort sort = null;

			if (ch == 'B' || ch == 'b') {
				sort = new BubbleSort();
			} else if (ch == 'H' || ch == 'h') {
				sort = new HeapSort();
			} else if (ch == 'Q' || ch == 'q') {
				sort = new QuickSort();
			} else {
				System.out.println("μ§€μ›λ˜μ§€ μ•ŠλŠ” κΈ°λŠ₯μž…λ‹ˆλ‹€.");
				return;
			}

			// μ •λ ¬ 방식과 상관 없이 Sort에 μ„ μ–Έν•œ λ©”μ„œλ“œ 호좜
			int[] arr = new int[10];
			sort.ascending(arr);
			sort.descending(arr);
			sort.description();
	}
}

결과값이 μ΄λ ‡κ²Œ λ‚˜μ˜€κ²Œ..

 

 

<λ‚΄κ°€ ν•œ 것>​

1. 일단 μ‹€ν–‰ 클래슀λ₯Ό ν† λŒ€λ‘œ λ‚˜λ¨Έμ§€ ν΄λž˜μŠ€λ“€μ„ λ§Œλ“€μ–΄μ€Œ. (Sort, BubbleSort, HeapSort, QuickSort)

​

2. κ°€μž₯ 기본이 될 Sort 클래슀λ₯Ό μΈν„°νŽ˜μ΄μŠ€λ‘œ μ„ μ–Έν•˜κ³  κΈ°λ³Έ 틀을 μ§°λ‹€.

package sorting;

public interface Sort {

	public void ascending(int[] arr);
	public void descending(int[] arr);
	
	default void description() {
		System.out.println("숫자λ₯Ό μ •λ ¬ν•˜λŠ” μ•Œκ³ λ¦¬μ¦˜μž…λ‹ˆλ‹€.");
	}
    
}

 

3. λ‚˜λ¨Έμ§€ 뢀뢄을 λ©”κΎΌλ‹€.

​

BubbleSort.java

package sorting;

public class BubbleSort implements Sort {

	@Override
	public void ascending(int[] arr) {
		System.out.println("BubbleSort ascending");
	}

	@Override
	public void descending(int[] arr) {
		System.out.println("BubbleSort descending");
	}
	
	@Override
	public void description() {
		Sort.super.description();
		System.out.println("BubbleSortμž…λ‹ˆλ‹€");
	}
    
}

 

 

HeapSort.java

package sorting;

public class HeapSort implements Sort {

	@Override
	public void ascending(int[] arr) {
		System.out.println("HeapSort ascending");
	}

	@Override
	public void descending(int[] arr) {
		System.out.println("HeapSort descending");
	}

	@Override
	public void description() {
		Sort.super.description(); // SortλŠ” μΈν„°νŽ˜μ΄μŠ€ 이름 κ΅¬λ³„μš©
		System.out.println("HeapSortμž…λ‹ˆλ‹€");
	}
    
}

 

 

QuickSort.java

package sorting;

public class QuickSort implements Sort {

	@Override
	public void ascending(int[] arr) {
		System.out.println("QuickSort ascending");
	}

	@Override
	public void descending(int[] arr) {
		System.out.println("QuickSort descending");
	}

	@Override
	public void description() {
		Sort.super.description();
		System.out.println("QuickSortμž…λ‹ˆλ‹€");
	}
    
}

 

 

 

심화) 좜λ ₯문을 λ°˜λ³΅ν•΄μ„œ λ‚˜μ˜€κ²Œ λ§Œλ“€μ–΄λ³΄λΌ.

​

<쑰건>

​

​

<λ‚΄κ°€ ν•œ 것> λ―Έμ™„μ„±

package sorting;

import java.io.IOException;

public class SortTest {
	public static void main(String[] args) throws IOException {

		while (true) {
			System.out.println("μ •λ ¬ 방식을 μ„ νƒν•˜μ„Έμš”.");
			System.out.println("B : BubbleSort ");
			System.out.println("H : HeapSort ");
			System.out.println("Q : QuickSort ");

			int ch = System.in.read();
			Sort sort = null;

			if (ch == 'B' || ch == 'b') {
				sort = new BubbleSort();
			} else if (ch == 'H' || ch == 'h') {
				sort = new HeapSort();
			} else if (ch == 'Q' || ch == 'q') {
				sort = new QuickSort();
			} else {
				System.out.println("μ§€μ›λ˜μ§€ μ•ŠλŠ” κΈ°λŠ₯μž…λ‹ˆλ‹€.");
				return;
			}

			// μ •λ ¬ 방식과 상관 없이 Sort에 μ„ μ–Έν•œ λ©”μ„œλ“œ 호좜
			int[] arr = new int[10];
			sort.ascending(arr);
			sort.descending(arr);
			sort.description();
			System.out.println();
		}
        
	}
}

<κ²°κ³Όκ°’>

-μ €λ ‡κ²Œ μ’…λ£Œλ˜μ–΄λ²„λ¦°λ‹€ γ…Žγ…Ž

​

더보기

β˜…μ„ μƒλ‹˜μ˜ ν”Όλ“œλ°±

μ €λ ‡κ²Œ ν•˜λŠ”κ²Œ λ§žλŠ”λ° int ch = System.in.read( ); μ–˜ λ•Œλ¬Έμ— 반볡이 μ•ˆλœλ‹€ν•¨. (이유λ₯Ό λ“€μ—ˆλŠ”λ° 이해가 μ•ˆκ°)

계속 μ‹€ν–‰μ‹œν‚€λ €λ©΄ ch = System.in.read( ); κ°’ 처리λ₯Ό 두 개 ν•΄ μ€˜μ•Όλœλ‹€κ³  ν•˜μ…¨λ‹€.

package sorting;

import java.io.IOException;

public class SortTest {
	public static void main(String[] args) throws IOException {

		while (true) {
			System.out.println("μ •λ ¬ 방식을 μ„ νƒν•˜μ„Έμš”.");
			System.out.println("B : BubbleSort ");
			System.out.println("H : HeapSort ");
			System.out.println("Q : QuickSort ");

			int ch = System.in.read();
			Sort sort = null;

			if (ch == 'B' || ch == 'b') {
				sort = new BubbleSort();
			} else if (ch == 'H' || ch == 'h') {
				sort = new HeapSort();
			} else if (ch == 'Q' || ch == 'q') {
				sort = new QuickSort();
			} else {
				System.out.println("μ§€μ›λ˜μ§€ μ•ŠλŠ” κΈ°λŠ₯μž…λ‹ˆλ‹€.");
				return;
			}

			// μ •λ ¬ 방식과 상관 없이 Sort에 μ„ μ–Έν•œ λ©”μ„œλ“œ 호좜
			int[] arr = new int[10];
			sort.ascending(arr);
			sort.descending(arr);
			sort.description();
			ch = System.in.read();
			ch = System.in.read();
			System.out.println();
		}
        
	}
}
λ°˜μ‘ν˜•

λŒ“κΈ€