Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags more
Archives
Today
Total
관리 메뉴

RussellHouse

[Java] 25. Interface 본문

Java

[Java] 25. Interface

러셀가의 집사 2017. 12. 23. 14:55



Interface





협업이 필수인 오늘과 같은 시대에 커뮤니케이션은 중요하다.



하지만 이러한 과정속에서도, 잘못된 커뮤니케이션으로 인하여 오류가 발생한다.



이를 보완하기 위한 한 가지 규제 도구로 Interface가 있다.


즉, 어떤 객체가 있고 그 객체가 특정한 인터페이스를 사용한다면



그 객체는 반드시 인터페이스의 메소드들을 구현해야 한다.


만약 인터페이스에서 강제하고 있는 메소드를 구현하지 않으면 해당 애플리케이션은 컴파일조차 되지 않는다.



다음 예제를 보자.



1
2
3
4
5
6
7
8
9
package org.opentutorials.javatutorials.interfaces.example1;
interface I{
public void z();
}
class A implements I{
public void z(){}
}




위는 클래스 A가 인터페이스 I를 구현하고 있으며,


인터페이스 I로 인해 z라는 메소드를 지정하고 있음을 알 수 있다.



인터페이스와 상속의 차이는,


상속의 경우가 상위 클래스의 기능을 하위 클래스가 물려 받는 것이라고 한다면,


인터페이스는 하위 클래스에 특정한 메소드가 반드시 존재하도록 강제하는 것을 의미한다.



또한, 상속의 경우는 extends를 사용하지만,


인터페이스의 경우는 implements를 사용한다.







실질적으로 인터페이스를 이용한 코드의 예시는 다음과 같다.


1) 인터페이스


1
2
3
4
5
6
7
package org.opentutorials.javatutorials.interfaces.example2;
public interface Calculatable {
public void setOprands(int first, int second, int third) ;
public int sum();
public int avg();
}




2) 인터페이스를 구현한 가짜 클래스를 사용하여 만든 에플리케이션




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package org.opentutorials.javatutorials.interfaces.example2;
class CalculatorDummy implements Calculatable{
public void setOprands(int first, int second, int third){
}
public int sum(){
return 60;
}
public int avg(){
return 20;
}
}
public class CalculatorConsumer {
public static void main(String[] args) {
CalculatorDummy c = new CalculatorDummy();
c.setOprands(10, 20, 30);
System.out.println(c.sum()+c.avg());
}
}




3) 인터페이스에 따라서 구현된 클래스




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package org.opentutorials.javatutorials.interfaces.example2;
class Calculator implements Calculatable {
int first, second, third;
public void setOprands(int first, int second, int third) {
this.first = first;
this.second = second;
this.third = third;
}
public int sum() {
return this.first + this.second + this.third;
}
public int avg() {
return (this.first + this.second + this.third) / 3;
}
}


4) 마지막으로 가짜 클래스인 CalculatorDummy를 실제 로직으로 교체하면 된다.



1
2
3
4
5
6
7
8
package org.opentutorials.javatutorials.interfaces.example2;
public class CalculatorConsumer {
public static void main(String[] args) {
Calculator c = new Calculator();
c.setOprands(10, 20, 30);
System.out.println(c.sum()+c.avg());
}
}





인터페이스는 유용한 도구이다.



인터페이스의 특징으로는 다음과 같은 것들이 있다.



하나의 클래스가 여러개의 인터페이스를 구현 할 수 있다. 

클래스 A는 메소드 x나 z 중 하나라도 구현하지 않으면 오류가 발생한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package org.opentutorials.javatutorials.interfaces.example3;
interface I1{
public void x();
}
interface I2{
public void z();
}
class A implements I1, I2{
public void x(){}
public void z(){}
}

인터페이스도 상속이 된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package org.opentutorials.javatutorials.interfaces.example3;
interface I3{
public void x();
}
interface I4 extends I3{
public void z();
}
class B implements I4{
public void x(){}
public void z(){}
}

인터페이스의 맴버는 반드시 public이다.

아래 코드는 오류를 발생한다. 

인터페이스는 그 인터페이스를 구현한 클래스를 어떻게 조작할 것인가를 규정한다. 

그렇기 때문에 외부에서 제어 할 수 있는 가장 개방적인 접근 제어자인 public만을 허용한다. 

public을 생략하면 접근 제어자 default가 되는 것이 아니라 public이 된다. 

왜냐하면 인터페이스의 맴버는 무조건 public이기 때문이다.

1
2
3
4
5
package org.opentutorials.javatutorials.interfaces.example3;
interface I5{
private void x();
}

abstract vs interface

인터페이스와 추상 클래스는 서로 비슷한 듯 다른 기능이다. 

인터페이스는 클래스가 아닌 인터페이스라는 고유한 형태를 가지고 있는 반면 

추상 클래스는 일반적인 클래스다. 

또한 인터페이스는 구체적인 로직이나 상태를 가지고 있을 수 없고, 

추상 클래스는 구체적인 로직이나 상태를 가지고 있을 수 있다.





'Java' 카테고리의 다른 글

[Java] 28. Exception  (0) 2017.12.24
[Java] 26. Polymorphism  (0) 2017.12.23
[Java] 24. Final  (0) 2017.12.23
[Java] 23. Abstract  (0) 2017.12.23
[Java] 22. Access Controler  (0) 2017.12.23
Comments