Interface
: 구현된 메소드는 가질 수 없다. 오직 상속을 통해서만 쓰인다. 객체생성이 안되므로 생성자가 없다.
package com.bit.day08.pm;
//인터페이스 : 객체 생성 x, 오직 추상 메소드만 가진다.
interface Inter01{
public abstract void func01();
}
interface Inter02{
public void func02(); //interface는 어차피 추상메소드만 가지기때문에 abstract 생략가능.
}
public class Ex10 implements Inter01{ //인터페이스의 상속 키워드 implements
public void func01(){} //추상메소드 오버라이딩
}
package com.bit.day08.pm;
//인터페이스 : 객체 생성 x, 필드도 x, 오직 추상 메소드만 가진다.
interface Inter01{
public abstract void func01();
}
interface Inter02{
public void func02();
//접근 제한자 생략. 안써도 public. 외부에서 접근하여 오버라이딩해야하니까
void func03();
}
public class Ex10 implements Inter01,Inter02 { //클래스가 아니기 때문에 다중상속을 허용한다.
public void func01(){}
public static void main(String[] args) {
}
@Override //어노테이션 : 오버라이드임을 명확히 한다.
public void func02() {
}
@Override
//public을 꼭 적어야한다. 생략하면 default가 되기 때문에 안된다.
public void func03() {
// TODO Auto-generated method stub
}
}
package com.bit.day08.pm;
//인터페이스들끼리의 상속은 extends이다.
//다중 상속도 가능하다.
interface Inter01 extends Inter02,Inter03{
public abstract void func01();
}
interface Inter02{
public void func02();
}
interface Inter03{
void func03();
}
public class Ex10 implements Inter01 {
public void func01(){}
public static void main(String[] args) {
}
@Override
public void func02() {
}
@Override
public void func03() {
// TODO Auto-generated method stub
}
}
인터페이스는 필드로 final만 가질 수 있다. 즉, 상수만 가질 수 있다.
non-static일 수가 없다 = 객체를 찍어내는 non-static일 수가 없다
-> static이 생략돼있다.
다시말해서, non static이란 말은 인스턴스 메소드를 가진다는 소리인데, 인터페이스는 인스턴스로 생성될 수 없으니까 non static을 가질 수가 없는것이다.
package com.bit.day08.pm;
interface Inter01 extends Inter02,Inter03{
public static final int su=1234; //상수는 값을 바꿀 수 없으니 선언 가능
public abstract void func01();
}
interface Inter02{
final int su2=2222; //static이 생략된 것이다. 어차피 다 public일테니 접근제한자도 생략 가능.
//또한 어차피 final일 것이기 때문에 생략 가능.
public void func02();
}
interface Inter03{
int su3=3333; //final이 생략된 것이다.
void func03();
}
public class Ex10 implements Inter01 {
public void func01(){}
public static void main(String[] args) {
System.out.println(Inter01.su);
// Inter03.su3=9999; 오류 : su3는 final이기 때문이다.
System.out.println(Inter01.su2);
System.out.println(Inter01.su3);
}
@Override
public void func02() {
}
@Override
public void func03() {
// TODO Auto-generated method stub
}
}
interface Inter04(){}
//메모로 사용가능하다. 설명하는 내용을 인터페이스의 이름으로 주고 상속받게하면 그 클래스에 대한 설명이 된다.
//또한 마킹의 역할 (?)