interface helloIn {
public String say() throws 임의의Exception;
}
public class A implements helloIn {
public String say() {// 여기에서 throws 임의의Exception을 안해줘도
컴파일이 되던데 왜 그렇죠?
.....
}
}
만약 그렇게 해야만 한다면 어떠한 복잡한 interface가 있는데 자신이 쓰지 않을
메쏘드들도 모두 overriding 한다면 얼마나 쓸데 없는 짓이겠습니까...
제 생각입니다...
"서용원" <seo_yo...@hotmail.com> wrote in message
news:aeetdn$5m5$1...@news1.kornet.net...
여러가지 예를 통해서 왜 그런지 알아보도록 하죠
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>...
"돌무더기" <dol-...@hanmail.net> wrote in message
news:NDDO8.3980$tR4...@news.hananet.net...