인터페이스 : 객체의 사용 방법을 정의한 타입
public interface InterfaceName {
// 상수 필드
public static final int CONSTANT = 10;
// 추상 메소드
public abstract void funtion();
}
상수 필드
public static final
이 붙는다.추상 메소드
public abstract
이 붙는다. 구현 클래스
개발코드 -> 인터페이스의 메소드 호출 -> 인터페이스는 객체의 메소드 호출
이때, 객체는 실체 메소드를 가져야하는데, 인터페이스에 선언된 추상 메소드와 동일한 실체 메소드를 가지고 있어야한다.
따라서, 구현 클래스 -> 구현 객체를 구현해야한다.
public class ImplementsClassName implements InterfaceName {
// 인터페이스에서 선언된 추상메소드의 실체 메소드
public void function(){
return;
}
}
인터페이스 사용 방법
public class Example{
public static void main(String[] args){
// 인터페이스 타입에 구현 객체를 대입한다.
InterfaceName in;
in = new InplementsClassName();
}
}
다중 인터페이스
public class ImplementsClassName implements Interface1, Interface2{
// interface1의 추상 메소드의 실체 메소드 구현
// interface2의 추상 메소드의 실체 메소드 구현
}
인터페이스로 구현객체 사용 방법
public class Example{
/* Field */
// 인터페이스가 필드에 선언될 경우, 구현 객체를 대입한다.
Interface1 in = new ImplementsClassName();
/*--------------------*/
/* Constructor */
Exmple(){
}
// 인터페이스가 생성자의 매개변수로 사용됨
Example(Interface1 in) {
this.in = in;
in.function();
}
// Example ex = new Example(new ImplementsClassName());
// 매개변수에 구현 객체를 넣어서 사용 가능하다.
/*--------------------*/
/* Method */
// 인터페이스 타입이 메소드의 로컬 변수 타입으로 사용될 때, 변수에 구현 객체를 넣는다.
void exampleMethod1(){
Interface1 in = new ImplementsClassName();
in.function();
}
// 인터페이스 타입이 메소드의 매개변수로 사용될 때, 매개변수에 구현 객체를 넣는다.
void exampleMethod2(Interface1 in) {
in.function();
}
/*--------------------*/
}
public class Application{
public static void main(String[] args){
// 필드에 선언된 인터페이스에 접근
Example ex1 = new Example();
ex.in.function();
// 생성자에 구현객체 넣기.
Example ex2 = new Example(new ImplementsClassName()):
// 메소드 불러오기 (메소드 안에 구현 객체의 함수 호출하는 기능)
Example ex3 = new Example();
ex3.exampleMethod1();
// 메소드 불러오기 (메소드의 매개변수로 구현 객체 넣어서 메소드 내의 구현 메소드 불러오기)
Example ex4 = new Example();
ex4.exampleMethod2(new ImplementsClassName());
}
}
Promotion : 구현 객체가 인터페이스 타입으로 변환되는 것은 자동 타입 변환이다.
Interface interfaceName = new ImplementsClassName();
public ImplementsClassName implements interfaceName{ }
이때, 구현 클래스의 자식클래스 역시 인터페이스 타입으로 Promotion 가능하다.
Interface vs Abstract Class
Parameter Polymorphism
Casting
interface Human {
void walk();
}
class JM implements Human{
void walk(){
System.out.println("달린다.");
}
void study(){
System.out.println("공부한다. ");
}
}
public class Example {
public static void main(String[] args){
Humane human = new JM();
human.walk();
human.study(); // 오류가 난다. Human 형에는 study 정의 안됨
JM jm = (JM)human;
jm.walk();
jm.study();
}
}
객체 타입 확인
if(Human instanceof JM) {
JM jm = (JM) human;
}
인터페이스 상속
public interface childInterface extends parentInterface1, parentInterface2 {}
클래스 내부에 선언한
class ClassName{
class NestedClassName{
}
interface NestedInterfaceName{
}
}
class OuterClass {
// Instance Member Class
class InstanceMemberClass {
InstanceMemberClass (){}
int field;
// static int field;
void method();
// static method();
}
// Static Member Class
class StaticMemberClass {
StaticMemberClass(){}
int field1;
static int field2;
void method1();
static method2();
}
// Local Member Class
void method(){
class LocalClass{
LocalClass(){}
int field;
// static int field;
void method();
// static method();
}
// 메소드 내세어 객체를 생성하고 사용해야 한다.
LocalClass lc = new LocalClass();
lc.field = 1;
lc.method();
}
}
class Application{
public static void main(String[] args){
// 인스턴스 멤버 클래스 선언 방법
OuterClass oc = new OuterClass();
OuterClass.InstanceMemberClass imc = oc.new InstanceMemberClass();
// 정적 멤버 클래스 선언 방법
OuterClass.InstanceMemberClass imc = new OuterClass.InstanceMemberClass();
// Outer Class에 선언된 로컬 클래스 생성하기
oc.method();
}
}
class OuterClass{
interface InterfaceName {
void method();
}
}
java.lang.ClassNotFoundException
java.lang.RuntimeException
NullPointerException : null값을 가지는 참조변수로 객체 접근 연산자를 사용하였기 때문에 예외가 발생한다.
ArrayIndexOutOfBoundsException : 배열세어 인덱스 범위를 초과할 경우, 5길이의 배열에서 arr[5]에 접근할때 에러가 발생하는 것과 같다.
NumerFormatException : 문자열로 되어 있는 데이터를 숫자로 변경하는 과정
Integer.parseInt(String str)
Double.parseDouble(String str)
여기서 숫자로 변환될 수 없는 문자가 포함되어 있으면 에러를 발생시킨다.
ClassCastException : 타입 캐스팅은 하위 클래스 -> 상위 클래스 or 구현 클래스 -> 인터페이스
try-catch-finally
try{
// 실행하다가 예외가 발생하면 catch로 넘어간다.
} catch(ExpcetionClass e) {
// 예외 처리를 여기서 해준다.
// try중 예외가 발생안하면 여기는 생략한다.
} finally {
// 항상 실행하는 구문이다.
// 앞에서 return이 있어서 항상 실행된다.
}
throws
void methodName() throws ExceptionClass1, ExceptionClass2 {
// 일반적인 메소드 내용, 여기서 예외가 발생하면
}
void methodName() throws Exception {
// Exception으로 설정하면, 모든 예외를 던질 수 있다.
}
public class Parent{
public int a = 1;
public int getA() return a;
public int setA(int a) this.a = a;
}
public static void main(String args[]){
// 일반적인 인스턴스 생성 방법
Parent parent = new Parent();
// 익명 클래스를 사용하여 인스턴스 생성
Parent test = new Parent(){
public int b = 10;
@Override
public int getB() return b;
@Override
public int setB() this.b = b
};
}