Live-Study

[8μ£Όμ°¨] μΈν„°νŽ˜μ΄μŠ€

youn12 2021. 2. 19. 16:13

 

✏️ Info.

- Java Interface ν•™μŠ΅

πŸ“‹ List.

1. μΈν„°νŽ˜μ΄μŠ€ μ •μ˜ν•˜λŠ” 방법
2. μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„ν•˜λŠ” 방법
3. μΈν„°νŽ˜μ΄μŠ€ 레퍼런슀λ₯Ό 톡해 κ΅¬ν˜„μ²΄λ₯Ό μ‚¬μš©ν•˜λŠ” 방법
4. μΈν„°νŽ˜μ΄μŠ€ 상속
5. μΈν„°νŽ˜μ΄μŠ€μ˜ κΈ°λ³Έ λ©”μ†Œλ“œ (Default Method), μžλ°” 8
6. μΈν„°νŽ˜μ΄μŠ€μ˜ static λ©”μ„œλ“œ, μžλ°” 8
7. μΈν„°νŽ˜μ΄μŠ€μ˜ private λ©”μ„œλ“œ, μžλ°” 9


βœ”οΈ Content.

 

1. μΈν„°νŽ˜μ΄μŠ€ μ •μ˜ν•˜λŠ” 방법

 

*μ•žμ„œ 배운 좔상 ν΄λž˜μŠ€λŠ” 좔상 λ©”μ„œλ“œλΏλ§Œ μ•„λ‹ˆλΌ 일반 ν΄λž˜μŠ€μ™€ 같이 μƒμ„±μž, ν•„λ“œ, 일반 λ©”μ„œλ“œλ₯Ό 생성할 수 μžˆμ—ˆλ‹€.

ν•˜μ§€λ§Œ μΈν„°νŽ˜μ΄μŠ€λŠ” μ˜€λ‘œμ§€ 좔상 'λ©”μ„œλ“œ'와 'μƒμˆ˜'λ§Œμ„ 포함할 수 μžˆλ‹€. ( Java 8 κΈ°μ€€)

 

  • μ˜ˆμ•½μ–΄λ‘œ classκ°€ μ•„λ‹Œ interface ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©
  • μ ‘κ·Ό μ œμ–΄μž
    • public
    • default
  • μΈν„°νŽ˜μ΄μŠ€ λ‚΄μ˜ λͺ¨λ“  λ³€μˆ˜λŠ” "public static final"둜 μ„ μ–Έλ˜λ©°, μƒλž΅ κ°€λŠ₯ν•˜λ‹€
  • μΈν„°νŽ˜μ΄μŠ€ λ‚΄μ˜ λͺ¨λ“  λ©”μ„œλ“œλŠ” "public abstract"둜 μ„ μ–Έλ˜λ©°, μƒλž΅ κ°€λŠ₯ν•˜λ‹€
public interface study {
    public static final String interfaceVal = "test";
    public abstract String interfaceMethod();
}

or

interface study {    
    String interfaceVal = "test";
    String interfaceMethod();
}

 

2. μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„ν•˜λŠ” 방법

 

 

*κ΅¬ν˜„ 방법 두 κ°€μ§€λ‘œ κ΅¬ν˜„ 클래슀λ₯Ό ν†΅ν•œ 방법과 읡λͺ… κ΅¬ν˜„ 객체λ₯Ό ν†΅ν•œ κ΅¬ν˜„μ΄ μžˆλ‹€.

 

 

κ΅¬ν˜„ 클래슀

  • μ„ μ–Έ ν›„μ—λŠ” interfaceμ—μ„œ μ„ μ–Έν•œ λ©”μ„œλ“œλ“€μ„ Overrideλ₯Ό 톡해 μ •μ˜ν•΄μ•Ό ν•œλ‹€.

 

ex) [class 이름] implements [interface 이름]

 

public class studyClass implements study{

    @Override
    public String interfaceMethod() {
        return null;
    }

    @Override
    public String name() {
        return null;
    }
}

 

읡λͺ… κ΅¬ν˜„ 객체

  • κ΅¬ν˜„ 클래슀λ₯Ό λ§Œλ“œλŠ” 것이 μΌλ°˜μ μ΄μ§€λ§Œ, μΌνšŒμ„±μ˜ 객체λ₯Ό μœ„ν•΄μ„œλŠ” λΉ„νš¨μœ¨μ μ΄λ―€λ‘œ ν•΄λ‹Ή 방법을 μ‚¬μš©ν•œλ‹€.

 

ex) [Interface 이름] [λ³€μˆ˜λͺ…] = new [Interface 이름]()

public class main {
    public static void main(String[] args) {
        study study = new study() {
            @Override
            public String interfaceMethod() {
                return null;
            }

            @Override
            public String name() {
                return null;
            }
        };
    }
}

 

3. μΈν„°νŽ˜μ΄μŠ€ 레퍼런슀λ₯Ό 톡해 κ΅¬ν˜„μ²΄λ₯Ό μ‚¬μš©ν•˜λŠ” 방법

 

 

* μΈν„°νŽ˜μ΄μŠ€ νƒ€μž…μœΌλ‘œ 객체λ₯Ό 생성할 수 있으며 ν•΄λ‹Ή 객체에 κ΅¬ν˜„ 클래슀둜 μΈμŠ€ν„΄μŠ€ν™” ν•  수 μžˆλ‹€.

 

  • μΈν„°νŽ˜μ΄μŠ€ νƒ€μž…μœΌλ‘œ μ„ μ–Έν•œ κ°μ²΄λŠ” κ΅¬ν˜„ 클래슀 λ‚΄μ—μ„œ μƒμ„±ν•œ λ©”μ„œλ“œ, ν•„λ“œλ₯Ό μ‚¬μš©ν•  수 μ—†λ‹€.
  • κ΄€λ ¨ λ‚΄μš©μ€ λ‹€μš΄ μΊμŠ€νŒ… λ˜λŠ” youn12.tistory.com/29(μƒμ†νŽΈ μ—μ„œ 일뢀 기술)을 확인

 

 

public class studyClass implements study{

    //κ΅¬ν˜„ 클래슀 λ©”μ†Œλ“œ
    public void say(){
        System.out.println("this studyClass");
    }

    @Override
    public String interfaceMethod() {
        return null;
    }

    @Override
    public String name() {
        return null;
    }
}


public static void main(String[] args) {
        study study = new studyClass();
        studyClass studyClass = new studyClass();

        study.say(); // λΆˆκ°€λŠ₯
        studyClass.say();
    }

 

 


 

4. μΈν„°νŽ˜μ΄μŠ€ 상속

 

 

* μΆ”μƒλ©”μ„œλ“œλŠ” 닀쀑 상속이 λΆˆκ°€λŠ₯ν•˜μ§€λ§Œ μΈν„°νŽ˜μ΄μŠ€λŠ” 닀쀑 상속이 κ°€λŠ₯ν•˜λ‹€.

 

 

public class studyClass implements study, study2{

    public void say(){
        System.out.println("this studyClass");
    }

    @Override
    public String interfaceMethod() {
        return null;
    }

    @Override
    public String name() {
        return null;
    }
}

 

* μΈν„°νŽ˜μ΄μŠ€ κ°„μ˜ μƒμ†μ—λŠ” extendsκ°€ μ‚¬μš©λœλ‹€.

interface study2 {
}

interface study extends study2{
}

 

5. μΈν„°νŽ˜μ΄μŠ€μ˜ κΈ°λ³Έ λ©”μ†Œλ“œ (Default Method), μžλ°” 8

 

 

* Java 8 μ΄μ „μ˜ Interface의 λ©”μ„œλ“œλŠ” μ„ μ–Έλ§Œ κ°€λŠ₯ν•˜λ©° κ΅¬ν˜„ν•  수 μ—†μ—ˆλ‹€. ν•˜μ§€λ§Œ Java 8 이후뢀터 Default λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ λ©”μ„œλ“œκ°€ κ΅¬ν˜„λΆ€λ₯Ό κ°€μ§ˆ 수 μžˆλ‹€.

  • default λ©”μ„œλ“œλŠ” μƒμ†λ°›λŠ” ν΄λž˜μŠ€μ—μ„œ ν•„μˆ˜λ‘œ κ΅¬ν˜„ν•˜μ§€ μ•Šμ•„λ„ λœλ‹€.

 

interface study {

    /* default λ©”μ†Œλ“œ*/
    default String nameCheck() {
        return "nameCheck";
    }
}

 

6. μΈν„°νŽ˜μ΄μŠ€μ˜ static λ©”μ„œλ“œ, μžλ°” 8

 

 

* Java 8λΆ€ν„° μƒκ²¨λ‚œ static λ©”μ„œλ“œλŠ” μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ΄μš©ν•˜μ—¬ κ°„λ‹¨ν•œ κΈ°λŠ₯을 κ°€μ§€λŠ” μœ ν‹Έλ¦¬ν‹° μ„± μΈν„°νŽ˜μ΄μŠ€λ₯Ό λ§Œλ“€ 수 있게 ν•œλ‹€.

 

  • 상속이 λΆˆκ°€λŠ₯ν•˜λ‹€.
  • μΈν„°νŽ˜μ΄μŠ€μ˜ μƒμˆ˜μ™€ 같은 ν˜•μ‹μœΌλ‘œ 쓰인닀.
  • [Interface 이름].[static λ©”μ†Œλ“œ 이름]으둜 μ‚¬μš©λœλ‹€.
interface study {
    
    /* static λ©”μ†Œλ“œ*/
    static String nameStaticCheck(){
        return "nameStaticCheck";
    }

}

public class main {
    public static void main(String[] args) {
    	/* static λ©”μ†Œλ“œ μ‚¬μš© (interface.method)*/
        System.out.println(study.nameStaticCheck());
    }
}

 

7. μΈν„°νŽ˜μ΄μŠ€μ˜ private λ©”μ„œλ“œ, μžλ°” 9

 

 

* μΈν„°νŽ˜μ΄μŠ€μ˜ λ©”μ„œλ“œλŠ” public이닀. Java 9λΆ€ν„°λŠ” privateλ₯Ό μ‚¬μš©ν•¨μœΌλ‘œμ¨ 외뢀에 κ³΅κ°œν•˜μ§€ μ•Šκ²Œ ν•˜λ©° μ½”λ“œμ˜ 쀑볡을 ν”Όν•˜κ³  Interface에 λŒ€ν•œ μΊ‘μŠν™”λ₯Ό μœ μ§€ν•  수 있게 ν•œλ‹€.

 

  • private, private static μ‚¬μš© κ°€λŠ₯ ν•˜μ§€λ§Œ 각각 호좜 κ°€λŠ₯ν•œ λ©”μ„œλ“œκ°€ λ‹€λ₯΄λ‹€. 
    • private method : private, abstract, default, static λ©”μ„œλ“œ 호좜 κ°€λŠ₯
    • private static method : static, private static λ©”μ„œλ“œλ§Œ 호좜 κ°€λŠ₯

❗️ Refer.

- μΈν„°νŽ˜μ΄μŠ€ μ •μ˜ν•˜λŠ” 방법
http://www.tcpschool.com/java/java_polymorphism_interface
https://www.notion.so/4b0cf3f6ff7549adb2951e27519fc0e6

- μΈν„°νŽ˜μ΄μŠ€μ˜ κΈ°λ³Έ λ©”μ†Œλ“œ (Default Method), μžλ°” 8, μΈν„°νŽ˜μ΄μŠ€μ˜ static λ©”μ„œλ“œ, μžλ°” 8
https://programmers.co.kr/learn/courses/5/lessons/241

-μΈν„°νŽ˜μ΄μŠ€μ˜ private λ©”μ†Œλ“œ, μžλ°” 9
https://flyburi.com/605

'Live-Study' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[10μ£Όμ°¨] λ©€ν‹°μ“°λ ˆλ“œ ν”„λ‘œκ·Έλž˜λ°  (0) 2021.02.19
[9μ£Όμ°¨] μ˜ˆμ™Έ 처리  (0) 2021.02.19
[7μ£Όμ°¨] νŒ¨ν‚€μ§€  (0) 2021.02.19
[6μ£Όμ°¨] 상속  (0) 2021.02.19
[5μ£Όμ°¨] 클래슀  (0) 2021.01.04