JAVA

15일차// 객체지향 프로그래밍 간단정리(7)✍// 인터페이스 interface

aesup 2021. 1. 29. 13:19
728x90

interface

인터페이스란 일종의 추상클래스이다.

추상클래스처럼 추상메소드를 갖지만 추상클래스보다 추상화 정독 높아서 추상클래스와 달리

몸통을 갖춘 일반 메서드 또는 멤버변수를 구성원으로 가질 수 없다.

오직 추상메서드와 상수만을 멤버로 가질 수 있다.

package main;

public class HeClass implements MyInterface {

    @Override
    public void method(int i) {
        System.out.println("HeClass method(int i)");
    }

}

MainClass

package main;

public class MainClass {
    public static void main(String[] args) {        
        /*
            abstract class : 일반메소드 + 추상메소드 + 멤버변수

            interface : 추상메소드
                        포함하는 메소드가 선언만 되어 있는 것.
                        다중 상속이 가능하다.
                        빠르게 클래스의 내용을 파악할 수 있는 점.
                        확장성, 관리목적을 갖고 있다.                    
        */

    //    MyInterface inf = new MyInterface();

        MyClass mycls = new MyClass();
        mycls.method(1);

        YouClass youcls = new YouClass();
        youcls.func();
        youcls.method(2);

        YouInterface yinter = new YouInterface() {            
            @Override
            public void func() {                
            }
        };

        MyInterface arr[] = new MyInterface[2];
        arr[0] = new MyClass();
        arr[1] = new HeClass();

        for (MyInterface in : arr) {
            in.method(1);
        }

        MyInterface my = new MyClass();

    }
}

package main;

public class MyClass implements MyInterface {

    @Override
    public void method(int i) {
        System.out.println("MyClass method(int i)");
    }

}

MyInterface

package main;

public interface MyInterface {

//    private int number;        변수 선언 불가

//    public void name() {}    일반 메소드 선언 불가

    public void method(int i);


}
728x90