Spring/JAVA

Java Interface

alsruds 2023. 10. 3. 23:44
๐Ÿฃ์ƒํ™œ์ฝ”๋”ฉ ๊ฐ•์˜๐Ÿฃ

Java Interface
https://www.youtube.com/playlist?list=PLuHgQVnccGMDiv-rCwlN-YjLKWR6-SBbM

 

  • ์ธํ„ฐํŽ˜์ด์Šค : ํด๋ž˜์Šค์˜ ํ˜•ํƒœ ๊ทœ์ •

 

  • ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ
    • ํด๋ž˜์Šค์˜ ๊ธฐ๋Šฅ์ด ๋งŽ์„ ๋•Œ
    • ํ˜‘์—… ์‹œ ๊ธฐ๋Šฅ์„ ๋ช…ํ™•ํžˆ ํ•ด์•ผํ•  ๋•Œ
    • ๊ณตํ†ต์ ์œผ๋กœ ๊ฐ€์ ธ์•ผ ํ•  ๋ณ€์ˆ˜๋‚˜ ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ์„ ๋•Œ

 

  • ๋‹คํ˜•์„ฑ (Polymorphism) : ํ•˜๋‚˜์˜ ํด๋ž˜์Šค๊ฐ€ ์—ฌ๋Ÿฌ ์–ผ๊ตด ๊ฐ–๊ธฐ

 

โœจ Interface

interface Calculable {
    double PI = 3.14; // ๋ณ€์ˆ˜ ์ •์˜ ์‹œ์—๋Š” ๊ฐ’ ์ •ํ•ด์ฃผ๊ธฐ
    int sum(int v1, int v2);
}
interface Printable {
    void print();
}

 

โœจ Class

· RealCal : Calculable, Printable ๊ตฌํ˜„

// ํ•˜๋‚˜์˜ ํด๋ž˜์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„ ๊ฐ€๋Šฅ
class RealCal implements Calculable, Printable {

    @Override
    public int sum(int v1, int v2) {
        return v1 + v2;
    }

    @Override
    public void print() {
        System.out.println("This is RealCal");
    }
}

 

· AdvancedPrint : Printable ๊ตฌํ˜„

class AdvancedPrint implements Printable {

    @Override
    public void print() {
        System.out.println("AdvancedPrint");
    }
}

 

โœจ Main

public class Main {
    public static void main(String[] args) {
        RealCal c = new RealCal();
        System.out.println(c.sum(2,1));
        c.print();
        System.out.println(c.PI);

        Printable p = new AdvancedPrint();
        p.print();
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ