자바
10 인터페이스
림가이드
2023. 2. 2. 16:46
> 인터페이스?
: 다중 상속처럼 사용할 수 있는 기능으로 추상메소드와 상수만으로 이루어짐
-> final: 데이터 값 변경 불가능
- 상속과 인터페이스 동시 사용?
: 동시 사용으로 다중 상속과 같은 효과를 볼 수 있음
package Interface;
interface School {
public static final int MAX_CLASS = 20;
public static final int MAX_PERSON_PER_CLASS = 40;
public abstract void printSchool();
}
class Student implements School {
public void printSchool() {
System.out.println("Seoul National University");
}
}
class Person {
public String name;
public void printName() {
System.out.println("Name: " + name);
}
}
class Student2 extends Person implements School {
Student2(String name) {
this.name = name;
}
@Override
public void printSchool() {
System.out.println("Harvard University");
}
}
public class Interface {
public static void main(String[] args) {
System.out.println("===인터페이스===");
Student s1 = new Student();
s1.printSchool(); // Seoul National University
System.out.println(s1.MAX_CLASS); // 20
System.out.println(s1.MAX_PERSON_PER_CLASS); // 40
System.out.println("\n===like 다중 상속===");
Student2 s2 = new Student2("김태희");
s2.printSchool(); // Harvard University
s2.printName(); // 김태희
}
}