wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another Interface Question.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another Interface Question.." Watch "Another Interface Question.." New topic
Author

Another Interface Question..

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561

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 ]

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Miki Muzsi
Ranch Hand

Joined: Jun 23, 2003
Posts: 120
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<br /> <br />SCJP 1.4, SCBCD 1.3
Miki Muzsi
Ranch Hand

Joined: Jun 23, 2003
Posts: 120
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

Joined: Nov 27, 2001
Posts: 1561
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

Joined: May 30, 2003
Posts: 443
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
 
subject: Another Interface Question..
 
Similar Threads
Override with exception questions
JQ+ Question ID :957711112180
Exceptions in Overriding methods??
Broader exceptions thrown by methods that implement interfaces
From JQPlus : QuestionID 957711112180