인터페이스는 아래 구성요소로만 이루어진다.
디폴트 메소드를 통해 기존 오픈소스등의 구현된 인터페이스에 새기능을 추가하고 기존 인터페이스와의 호환을 위해 생겨났다.Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.
interface TestInterface {
default void print() {
System.out.println("hello");
}
}public interface Foo {
static void buzz() {
System.out.print("Hello");
staticBaz();
}
private static void staticBaz() {
System.out.println(" static world!");
}
}
public class CustomFoo implements Foo {
public static void main(String... args) {
Foo.buzz();
}
}
public interface Foo {
default void bar() {
System.out.print("Hello");
baz();
}
private void baz() {
System.out.println(" world!");
}
}
public class CustomFoo implements Foo {
public static void main(String... args) {
Foo customFoo = new CustomFoo();
customFoo.bar();
}
}
public abstract class Member {
public abtract boolean addMember(MemberDTO member);
}
Class 'MemberManager' must either be declared abstract or implement abstract method 'addMember(MemberDTO)' in 'MemberManager'
기본 자료형과 마찬가지로 참조 자료형도 두 번 이상 값을 할당하거나 새로 생성자를 사용하여 초기화 할 수 없다.
객체의 안에있는 객체들은 final로 선언된 것이 아니므로 값의 변경이 불가한것은 아니다.
/**
@author jhkim
@since 2023/06/17
*/
public class FinalReferenceType {
final MemberDTO dto = new MemberDTO();
public static void main(String[] args) {
FinalReferenceType type = new FinalReferenceType();
type.checkDTO();
}
public void checkDTO() {
System.out.println(dto);//MemberDTO{name='null'}
dto.setName("test");
System.out.println(dto);//MemberDTO{name='test'}
}
}
참고
https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html
Default Methods (The Java™ Tutorials >
Learning the Java Language > Interfaces and Inheritance)