ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java] μžλ°” μΈν„°νŽ˜μ΄μŠ€(Interface) μ‚¬μš©
    Android/Java 2021. 1. 24. 02:47
    λ°˜μ‘ν˜•
    • 이 글은 "μžλ°” 온라인 μŠ€ν„°λ””" λ‚΄μš©μ„ κ³΅λΆ€ν•˜μ—¬ μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

    μΈν„°νŽ˜μ΄μŠ€(Interface)λž€?

    • ν˜„μ—…μ—μ„œ μ†ŒμŠ€μ½”λ“œ μž‘μ„± μ‹œ 클래슀λ₯Ό μ²˜μŒλΆ€ν„° κ΅¬ν˜„ν•˜κ²Œ λœλ‹€λ©΄ μ½”λ“œμ˜ 가독성도 떨어지고 μ‹œκ°„λ„ 였래걸릴 κ²ƒμž…λ‹ˆλ‹€. λ˜ν•œ νŒ€λ³„λ‘œ 각각 λ‹€λ₯Έ λͺ¨λ“ˆμ„ λ§Œλ“€ μ‹œ νŒ€λ§ˆλ‹€ 방법이 λ‹€λ₯΄κΈ° λ•Œλ¬Έμ— μ„œλ‘œ ν˜Έν™˜μ„±μ΄ μ—†κ³  일관성이 λ–¨μ–΄μ§€κ²Œ λ©λ‹ˆλ‹€.

    • μΈν„°νŽ˜μ΄μŠ€λ₯Ό 톡해 기본적으둜 κ΅¬ν˜„ν•΄μ•Ό ν•  λ©”μ†Œλ“œμ— λŒ€ν•œ κ·œκ²©λ“€μ„ μ•Œλ €μ€€λ‹€λ©΄ 훨씬 효율적일 κ²ƒμž…λ‹ˆλ‹€.

    • 즉, μΈν„°νŽ˜μ΄μŠ€λž€ νŠΉμ • κΈ°λŠ₯을 κ°œλ°œν•˜λŠ”λ° μžˆμ–΄ 곡톡적인 κΈ°λŠ₯을 λͺ…μ‹œν•˜κ³  κ°•μ œμ μœΌλ‘œ κ΅¬ν˜„ν•˜κ²Œλ” ν•˜λŠ” μ—­ν• μž…λ‹ˆλ‹€.

    • μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•˜κ²Œ λœλ‹€λ©΄ μžλ°”μ˜ λ‹€ν˜•μ„±μ„ μ΄μš©ν•  수 있고 이λ₯Ό 톡해 μœ μ§€λ³΄μˆ˜μ„±μ„ 높일 수 μžˆμŠ΅λ‹ˆλ‹€.


    μΈν„°νŽ˜μ΄μŠ€ μ •μ˜

    public interface Phone {
        int version = 12;
    
        String getManufacturer();
        String getOS();
    }
    • μΈν„°νŽ˜μ΄μŠ€λŠ” interfaceλΌλŠ” ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄μ„œ μ •μ˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
    • μ ‘κ·Όμ œμ–΄μžλ‘œ public λ˜λŠ” defaultκ°€ λͺ…μ‹œλ  수 μžˆμŠ΅λ‹ˆλ‹€.
    • μΈν„°νŽ˜μ΄μŠ€μ˜ λ³€μˆ˜μ˜ 값은 "public static final" ν‚€μ›Œλ“œκ°€ μƒλž΅λœ μƒμˆ˜μ΄λ―€λ‘œ 주어진 κ°’μœΌλ‘œλ§Œ μ‚¬μš©κ°€λŠ₯ ν•©λ‹ˆλ‹€.
    • getManufactureκ³Ό getOSλŠ” "public abstract" ν‚€μ›Œλ“œκ°€ μƒλž΅λœ λ©”μ†Œλ“œλ‘œ μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•˜λŠ” κ³³μ—μ„œ μ˜€λ²„λΌμ΄λ”©μ„ κ°•μ œμ‹œν‚΅λ‹ˆλ‹€.

    μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„

    public class IPhone implements Phone{
        @Override
        public String getManufacturer() {
            return "Apple";
        }
    
        @Override
        public String getOS() {
            return "MacOS";
        }
    }
    public class Galaxy implements Phone{
        @Override
        public String getManufacturer() {
            return "Samsung";
        }
    
        @Override
        public String getOS() {
            return "Android";
        }
    }
    • μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„μ€ implements ν‚€μ›Œλ“œλ₯Ό 톡해 일반 ν΄λž˜μŠ€μ—μ„œ μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•©λ‹ˆλ‹€.
    • IPhone ν΄λž˜μŠ€μ™€ Galaxy ν΄λž˜μŠ€λŠ” Phone μΈν„°νŽ˜μ΄μŠ€μ˜ κ·œκ²©μ— 맞게 μž¬μ •μ˜ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

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

    public class Main {
        public static void main(String[] args) {
            Phone galaxy= new Galaxy();
            Phone iphone = new IPhone();
    
            System.out.println(galaxy.getManufacturer());
            System.out.println(galaxy.getOS());
    
            System.out.println(iphone.getManufacturer());
            System.out.println(iphone.getOS());
        }
    }

    • μžλ°”μ˜ λ‹€ν˜•μ„±μ„ μ΄μš©ν•˜μ—¬ μΈν„°νŽ˜μ΄μŠ€ μ°Έμ‘°λ³€μˆ˜λ‘œ λ‹€μ–‘ν•œ 클래슀λ₯Ό ν‘œν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
    • μ˜ˆμ‹œμ—μ„œ λ³΄λŠ” κ²ƒμ²˜λŸΌ Phone μΈν„°νŽ˜μ΄μŠ€ μ°Έμ‘°λ³€μˆ˜μ— Galaxy ν΄λž˜μŠ€μ™€ IPhone 클래슀둜 μ—¬λŸ¬ νƒ€μž…μ˜ 객체둜 μ°Έμ‘°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

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

    • μΈν„°νŽ˜μ΄μŠ€λŠ” 닀쀑 상속이 κ°€λŠ₯ν•˜μ—¬ μ—¬λŸ¬ μΈν„°νŽ˜μ΄μŠ€λ₯Ό 상속받아 ν•˜λ‚˜μ˜ μΈν„°νŽ˜μ΄μŠ€λ₯Ό λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.
    • 이럴경우 λͺ¨λ“  μΈν„°νŽ˜μ΄μŠ€μ— μ‘΄μž¬ν•˜λŠ” λ©”μ†Œλ“œλ₯Ό μž¬μ •μ˜ ν•΄μ•Ό ν•©λ‹ˆλ‹€.
    public interface Phone {
        int version = 12;
    
        String getManufacturer();
        String getOS();
    }
    
    public interface Watch {
        String getAppearance();
    }
    
    public interface Device extends Phone,Watch{
    }
    public class Machine implements Device{
        @Override
        public String getManufacturer() {
            return null;
        }
    
        @Override
        public String getOS() {
            return null;
        }
    
        @Override
        public String getAppearance() {
            return null;
        }
    }
    • Phoneκ³Ό Watch μΈν„°νŽ˜μ΄μŠ€λ₯Ό 상속받은 Device μΈν„°νŽ˜μ΄μŠ€λ₯Ό μƒμ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
    • Device μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•˜λŠ” 일반 ν΄λž˜μŠ€μ—μ„œλŠ” Phoen μΈν„°νŽ˜μ΄μŠ€μ™€ Watch μΈν„°νŽ˜μ΄μŠ€μ— μžˆλŠ” λ©”μ†Œλ“œλ“€μ„ λͺ¨λ‘ κ΅¬ν˜„ν•΄μ•Όλ§Œ ν•©λ‹ˆλ‹€.

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

    • μžλ°” 8 μ΄μ „μ˜ μΈν„°νŽ˜μ΄μŠ€μ—λ₯Ό μ •μ˜ν•  λ•ŒλŠ” λ©”μ†Œλ“œλ₯Ό μ„ μ–Έλ§Œ ν•  뿐 λ‚΄λΆ€μ—μ„œ κ΅¬ν˜„λ˜λŠ” μ½”λ“œλŠ” μ—†μ—ˆμŠ΅λ‹ˆλ‹€.
    • ν•˜μ§€λ§Œ μžλ°” 8 이후 default λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•˜λ©΄ μΈν„°νŽ˜μ΄μŠ€ λ‚΄λΆ€μ—μ„œ μ½”λ“œκ°€ μ‘΄μž¬ν•  수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.
    • default λ©”μ†Œλ“œλŠ” μ˜€λ²„λΌμ΄λ”©μ΄ κ°•μ œμ μ΄ μ•„λ‹Œ μ„ νƒμ μ΄λΌμ„œ μ˜€λ²„λΌμ΄λ”©μ„ ν•˜μ§€ μ•Šμ„ μ‹œ κΈ°λ³Έ κ΅¬ν˜„λœ μ½”λ“œκ°€ μ‹€ν–‰λ©λ‹ˆλ‹€.
    • default ν‚€μ›Œλ“œλŠ” 무쑰건 λͺ…μ‹œν•΄μ•Ό ν•©λ‹ˆλ‹€.
    • ν•˜μœ„ν˜Έν™˜μ„±μ„ μœ„ν•΄ 주둜 μ‚¬μš©ν•œλ‹€κ³  ν•©λ‹ˆλ‹€.
    public interface Phone {
        int version = 12;
    
        String getManufacturer();
        String getOS();
    
        default void printRing(){
            System.out.println("Sound : Ring Ring~~");
        }
    }
    public class Galaxy implements Phone{
        @Override
        public String getManufacturer() {
            return "Samsung";
        }
    
        @Override
        public String getOS() {
            return "Android";
        }
    
        @Override
        public void printRing() {
            System.out.println("Sound : Ring~~~~~~~");
        }
    }
    • Phone μΈν„°νŽ˜μ΄μŠ€μ— printRingμ΄λΌλŠ” default λ©”μ†Œλ“œλ₯Ό λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€.
    • μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•˜λŠ” ν΄λž˜μŠ€μ—μ„œ μ„ νƒμ μœΌλ‘œ μ˜€λ²„λΌμ΄λ”©μ„ ν•  수 있고 Galaxy ν΄λž˜μŠ€μ—μ„œλŠ” μž¬μ •μ˜λ₯Ό, Iphone ν΄λž˜μŠ€μ—μ„œλŠ” κ·ΈλŒ€λ‘œ μ‚¬μš©ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
    public class Main {
        public static void main(String[] args) {
            Phone galaxy= new Galaxy();
            Phone iphone = new IPhone();
    
            System.out.println(galaxy.getManufacturer());
            System.out.println(galaxy.getOS());
            galaxy.printRing();
    
            System.out.println();
            System.out.println(iphone.getManufacturer());
            System.out.println(iphone.getOS());
            iphone.printRing();
    
        }
    }


    μΈν„°νŽ˜μ΄μŠ€ static λ©”μ†Œλ“œ(μžλ°” 8)

    • μžλ°” 8이후에 default λ©”μ†Œλ“œμ™€ λ§ˆμ°¬κ°€μ§€λ‘œ static method도 μΆ”κ°€λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
    • static methodλŠ” μΈμŠ€ν„΄μŠ€ 생성 없이 μΈν„°νŽ˜μ΄μŠ€ 이름을 ν†΅ν•΄μ„œ 직접 λ©”μ†Œλ“œ 호좜이 κ°€λŠ₯ν•©λ‹ˆλ‹€.
    • μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•˜λŠ” ν΄λž˜μŠ€μ—μ„œ μž¬μ •μ˜κ°€ λΆˆκ°€λŠ₯ν•©λ‹ˆλ‹€.
    public interface Phone {
        int version = 12;
    
        String getManufacturer();
        String getOS();
    
        default void printRing(){
            System.out.println("Sound : Ring Ring~~");
        }
    
        static void printUser(String user){
            System.out.println("User : "+user);
        }
    }
    public class Main {
        public static void main(String[] args) {
            Phone galaxy= new Galaxy();
            Phone iphone = new IPhone();
    
            System.out.println(galaxy.getManufacturer());
            System.out.println(galaxy.getOS());
            galaxy.printRing();
    
    
            System.out.println();
            System.out.println(iphone.getManufacturer());
            System.out.println(iphone.getOS());
            iphone.printRing();
    
            // static λ©”μ†Œλ“œ μ‚¬μš©
            Phone.printUser("Hong Gil Dong");
        }
    }


    μΈν„°νŽ˜μ΄μŠ€ private λ©”μ†Œλ“œ(μžλ°” 9)

    • μžλ°” 8 이후 μΈν„°νŽ˜μ΄μŠ€ λ‚΄λΆ€μ—μ„œ μ½”λ“œλ₯Ό κ΅¬ν˜„ν•˜λŠ” 것이 μΆ”κ°€λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
    • μ½”λ“œ κ΅¬ν˜„ μ‹œ λ³΅μž‘ν•œ μ½”λ“œλ₯Ό ν•œ λ²ˆμ— κ΅¬ν˜„ν•˜λŠ” κ²ƒλ³΄λ‹€λŠ” μž‘μ€ λ©”μ†Œλ“œλ₯Ό μž‘μ„±ν•˜μ—¬ λ³΅μž‘ν•œ λ©”μ†Œλ“œλ₯Ό κ΅¬ν˜„ν•˜λŠ” 것이 μž¬μ‚¬μš©, μœ μ§€λ³΄μˆ˜ μΈ‘λ©΄μ—μ„œ νš¨μœ¨μ μž…λ‹ˆλ‹€.
    • private λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•˜μ—¬ μž‘μ€ λ©”μ†Œλ“œλ₯Ό κ΅¬ν˜„ν•˜λ˜ μ™ΈλΆ€μ—μ„œ λ³Ό 수 μ—†κ³  μ‚¬μš©ν•  수 없도둝 ν• κ²Œ λ§Œλ“œλŠ” λ©”μ†Œλ“œμž…λ‹ˆλ‹€.
    public interface Phone {
        int version = 12;
    
        String getManufacturer();
        String getOS();
    
        default void printRing(){
            // private λ©”μ†Œλ“œ μ‚¬μš©
            printInfo();
            System.out.println("Sound : Ring Ring~~");
        }
    
        static void printUser(String user){
            // static private λ©”μ†Œλ“œ μ‚¬μš©
            getUser(user);
        }
    
        private void printInfo(){
            System.out.println("Print Information");
        }
    
        private static void getUser(String user){
            System.out.println("User : "+user);
        }
    }
    public class Main {
        public static void main(String[] args) {
            Phone galaxy= new Galaxy();
            Phone iphone = new IPhone();
    
            System.out.println(galaxy.getManufacturer());
            System.out.println(galaxy.getOS());
            galaxy.printRing();
    
    
            System.out.println();
            System.out.println(iphone.getManufacturer());
            System.out.println(iphone.getOS());
            iphone.printRing();
    
            Phone.printUser("Hong Gil Dong");
        }
    }


    μΆ”μƒν΄λž˜μŠ€μ™€ μΈν„°νŽ˜μ΄μŠ€ 비ꡐ

    • μΈν„°νŽ˜μ΄μŠ€λŠ” νŠΉμ • κΈ°λŠ₯을 κ΅¬ν˜„ν•˜κΈ° μœ„ν•œ ν•„μˆ˜ κ·œκ²©μ„ μ£ΌλŠ” κ²ƒμœΌλ‘œ μΆ”μƒν΄λž˜μŠ€λŠ” λ―Έμ™„μ„± 섀계도, μΈν„°νŽ˜μ΄μŠ€λŠ” κΈ°λ³Έ 섀계도라고 λ³΄μ‹œλ©΄ λ©λ‹ˆλ‹€.

    • λͺ¨λ“  클래슀λ₯Ό μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•œλ‹€λ©΄ κ³΅ν†΅λœ κΈ°λŠ₯을 λͺ¨λ‘ κ΅¬ν˜„ν•΄μ•Ό ν•˜κΈ° λ•Œλ¬Έμ— 번거둜운 μž‘μ—…μž…λ‹ˆλ‹€. 이럴경우 μΆ”μƒν΄λž˜μŠ€λ₯Ό μ΄μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€.

    • ν•˜μ§€λ§Œ, μΆ”μƒν΄λž˜μŠ€λ§Œ μ‚¬μš©ν•˜κΈ°μ—λŠ” 닀쀑상속이 λΆˆκ°€ν•˜κΈ° λ•Œλ¬Έμ— 각각 λ‹€λ₯Έ μΆ”μƒν΄λž˜μŠ€λ₯Ό κ΅¬ν˜„ν•˜λŠ”λ° κ³΅ν†΅λœ κΈ°λŠ₯이 μ‘΄μž¬ν•  κ²½μš°λŠ” μΈν„°νŽ˜μ΄μŠ€κ°€ ν•„μš”ν•©λ‹ˆλ‹€.

    • μ°Έμ‘°(https://myjamong.tistory.com/1500)

    λ°˜μ‘ν˜•
Designed by Tistory.