Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another Interface Question..

 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What methods have to be implemented by class L that says it implements I1 and I2?
a) pubic void m1() throws Exception
b) the class cannot implement both the interfaces as they have conflicting names
c) public void m1() throws SQLException, IOException;
d)public void m1()
the answer is....
Option D
I had no idea about this one, so I guessed it could be option A, since Exception is the parent of these 2 exceptions.
any comments?
[ July 02, 2003: Message edited by: Andres Gonzalez ]
[ July 02, 2003: Message edited by: Andres Gonzalez ]
 
Miki Muzsi
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your class L must implement CORRECTLY both interfaces. The return type and the arguments of m1 in both interfaces are the same. So we do not have conflicts (so b is incorrect).
Next step is to analyse the thrown exceptioins. In order to have class L correctly implementing I1, the method m1() (from L) must not throw wider than IOException (so it can not throw Exception => answer a and c are incorrect). the same storry for implementing m1 from I2.
Answer d is correct one, since public void m1() {} (from class L) implements correctly both interfaces (since it does not declare that it throws wider Exceptions than the implemented methods)!
Just as additional comment: if interface I1 is changed to,
interface I1{
void m1() throws Exception;
}
than a correct implementation of both I1 and I2 would be also:
class L2 implements I1, I2{
public void m1() throws SQLException {}
}
Hope it helps.
Miki
 
Miki Muzsi
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Related to this might be worthy to mention that both interfaces can declare a member variable with the same name (e.g i), and both variables are visible in a class that implements both interfaces. Just that is not possible to access the member by its name (sine it would be ambiguous), only prefixing it with the interface name.
e.g.
interface I1 {
int i = 9;
}
interface I2 {
int i = 10;
}
class L implements I1, I2{
L() {
System.out.println(I1.i); // prints 9
System.out.println(I2.i); // prints 10
System.out.println(i); // compiler error; which i to take?
}
}
Miki
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Miki.

Just as additional comment: if interface I1 is changed to,
interface I1{
void m1() throws Exception;
}
than a correct implementation of both I1 and I2 would be also:
class L2 implements I1, I2{
public void m1() throws SQLException {}
}

What happens if the method implemented in L2 throws an Exception that is not a SQLException or any subclass of it (I can't think of any other at the moment)?
Here I'm declaring that my method m1() might throw an SQLException, right? How is that handled? is it propagated to the caller of method m1()?
thanks again
[ July 02, 2003: Message edited by: Andres Gonzalez ]
 
Alton Hernandez
Ranch Hand
Posts: 443
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andres Gonzalez:
What happens if the method implemented in L2 throws an Exception that is not a SQLException or any subclass of it (I can't think of any other at the moment)?
[ July 02, 2003: Message edited by: Andres Gonzalez ][/QB]

There is nothing special about that implementation. It still has to follow the same rule i.e. it must throw the same or a subclass of the exception declared in the 'throws' clause. So in your example, it will fail during compilation.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic