Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

인터페이스와 implements한 클래스에 대해서...

456 views
Skip to first unread message

서용원

unread,
Jun 15, 2002, 4:39:38 AM6/15/02
to
안녕하세여!!! 궁금한게 있어서리...
예를 들어

interface helloIn {
public String say() throws 임의의Exception;
}
public class A implements helloIn {
public String say() {// 여기에서 throws 임의의Exception을 안해줘도
컴파일이 되던데 왜 그렇죠?
.....
}
}


돌무더기

unread,
Jun 15, 2002, 5:18:40 AM6/15/02
to
꼭 interface에 있는 메소드들을 모두 다 override할 필요는 없는 것 같은데요...

만약 그렇게 해야만 한다면 어떠한 복잡한 interface가 있는데 자신이 쓰지 않을

메쏘드들도 모두 overriding 한다면 얼마나 쓸데 없는 짓이겠습니까...

제 생각입니다...


"서용원" <seo_yo...@hotmail.com> wrote in message
news:aeetdn$5m5$1...@news1.kornet.net...

BMK

unread,
Jun 17, 2002, 7:35:04 PM6/17/02
to
아주 재밋는 주제를 던져주셔서.. 덕분에 흥미롭게 여러가지를 찾아봤네요..^^
감사..~~~

여러가지 예를 통해서 왜 그런지 알아보도록 하죠
1.
interface helloIn
{
public String say() throws Exception;


}
public class A implements helloIn
{
public String say()
{

return "AAA";
}
}
-> 컴파일이 잘 되는군요...

2.
interface helloIn
{
public String say() throws Exception;
public String say2() throws Exception;


}
public class A implements helloIn
{
public String say()
{

return "AAA";
}
}
->
A.java:6: A should be declared abstract; it does not define say2() in A
인터페이스의 모든 함수는 다 재구현을 해주어야한다는거겠죠??
그래서 리스너쓸때 이게 싫어서 Adapter쓰쟎아요...^^

3.
import java.io.*;

interface helloIn
{
public String say() throws IOException;


}
public class A implements helloIn
{

public String say() throws Exception
{
return "AAA";

}

}
-> helloIn 에서는 IOException을, A에서는 IOException보다 상위에 있는
Exception을 발생시키는데요... 이 결과는??
A.java:8: say() in A cannot implement say() in helloIn; overridden method does
not throw java.lang.Exception....
상속하는 클래스의 함수에서는 아마도 수퍼클래스(인터페이스)보다 많은 예외상황을
발생할수 없는게 아닌지....

4.
import java.io.*;

interface helloIn
{
public String say() throws IOException;


}
public class A implements helloIn
{

public String say() throws IOException, NoSuchFieldException
{
return "AAA";

}

}
-> A.java:8: say() in A cannot implement say() in helloIn; overridden method
does not throw java.lang.NoSuchFieldException

대충 명확해졌네요...

상속하는 클래스에서... 상속한 함수는 상속되는 함수가 발생하는 예외상황보다
많은 예외상황을 발생할수가 없네요...이건 지극히 당연한건데요...
위의 예제로 설명해보면...

helloIn a = aaa.getInterface(); // helloIn 를 implements하는 A
객체를 리턴하는 함수라고 할때...
try{
a.say();
}catch(IOException e)........

이런 식으로 진행될테구요... (helloIn 의 say는 IOException 만 발생하니깐..)
그렇다면 A의 say에서 발생되는 NoSuchFieldException는 ....?? ^^

주저리주저리...^^
대충 이해가 되시는지....

아 참... 생성자...에 관해서는 또 다른 경우가 적용된답니다...
그건 직접 고민해보심도 좋을듯...^^


"????" <seo_yo...@hotmail.com> wrote in message news:<aeetdn$5m5$1...@news1.kornet.net>...

Chang, Yoon Ki

unread,
Jul 18, 2002, 4:20:30 AM7/18/02
to
interface에 정의된 메소드는 abstract 클래스가 아닌 이상은 반드시 override
해주어야 합니다.

"돌무더기" <dol-...@hanmail.net> wrote in message
news:NDDO8.3980$tR4...@news.hananet.net...

JungHo Lim

unread,
Jul 30, 2002, 5:49:29 AM7/30/02
to
당연히 abstract가 아닌 이상 모두 override해주어야 합니다.
그것이 귀찮고 번거롭기 때문에 adapter 방식을 사용하는거죠..
java에서 event driven을 할 때 어떻게 하는지 생각해 보시면 알 수 있을겁니다.


0 new messages