BACK언어/JAVA
인터페이스란
15881588개발개발
2022. 8. 29. 11:23
interface정리
- 인터페이스란
- 추상클래스이다.
- 추상클래스보다는 추상화 정도가 높아서 추상클래스와 달리 몸통을 갖춘 일반메서드 또는 멤버변수를 구성원으로 가질 수 없다 .
- 인터페이스는 구현된것은 아무것도 없고 밑그림만 그려져 있는 기본설계도라고 보면 된다
추상클래스는 부분적으로만 완성된 미완성 설계도이다.
-인터페이스의 작성
- class 대신 interface 로 사용한다.
- public interface BasicInformationSettingRepository{ public static final 타입상수이름 =값; public abstract 메서드이름(매개변수 목록);
- 인터페이스 멤버들의 제약사항이 있다.
- 제약사항
- 모든 멤버변수는 public static final이어야 하며 이를 생략할 수 있다.
- 모든 메서드는 public abstract이어야 하며 이를 생략할 수 있다.
다같다.public static final int test =1 ; final int test = 1; static int test = 1; int test =1 ; public abstract String getTest(); String getTest();
- 제약사항
-인터페이스의 상속
- 다중상속이 가능하다 . (클래스 상속은 불가능하다 인터페이스만 가능함.
public interface SettingRepository extends JpaRepository<BasicInformationSetting, Long>, QuerydslPredicateExecutor<BasicInformationSetting> {
- 조상 인터페이스에 정의된 멤버를 모두 상속받는다 .
- SettingRepository 에는 정의된 것이 하나도 없지만 JpaRepository에 정의된 메소드등을 가지고 있다.
-인터페이스 구현
- 만일 구현해야 하는 인터페이스 중 일부만 구현한다면 , abstract를 붙여 추상클래스로 선언해야 한다.
public class Test1 implements testInterface{
@Override
public String testMethod() {
return null;
}
}
public interface testInterface {
int testint=1;
String testMethod();
}
이렇게 구현해야하지만 (인터페이스에 구현된 모든 메소드들을 override 해와야 하지만 )
public interface testInterface {
int testint=1;
String testMethod();
String testMethod2();
}
**abstract** classTest1implementstestInterface{
}
abstract로 구현하면 method를 overiding안해도 에러가 안남.