This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can anyone explain me this? 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 "can anyone explain me this?" Watch "can anyone explain me this?" New topic
Author

can anyone explain me this?

M Mistroni
Ranch Hand

Joined: Oct 03, 2005
Posts: 62
hello,
i was going thru some mock exams and i found following question

ead the following code below.

public interface AQuestion
{
public abstract void someMethod() throws Exception;
}

A Class implementing this interface should

1. Necessarily be an abstract class.
2. Should have the method public abstract void someMethod();
3. Should have the method public void someMethod() which has to throw an exception which is a subclass of java.lang.Exception.
4. Should have the method public void someMethod() which need not throw an Exception.


i choosed 3, but answer was 4... can anyone explain me why?

thanks and regards
marco
Arun Thakur
Greenhorn

Joined: Mar 15, 2006
Posts: 17
hi there,

I beleive that answer will be 4 as u said.
B'coz the class implementing that interface not necessaryly threw a exception.
J Sato
Ranch Hand

Joined: Mar 30, 2006
Posts: 40
4 is correct, the class implementing someMethod() cannot declare narrower checked exceptions and does not have to declare any exceptions.

Consider the following:


The first someMethod() does not throw any exceptions. The second someMethod() throws Exception which is the narrowest checked exception that can be thrown. The third someMethod() throws IOException and will not compile.
[ April 10, 2006: Message edited by: J Sato ]
Sreeram Desigan
Greenhorn

Joined: Apr 07, 2006
Posts: 23
Originally posted by J Sato:
Consider the following:


The first someMethod() does not throw any exceptions. The second someMethod() throws Exception which is the narrowest checked exception that can be thrown. The third someMethod() throws IOException and will not compile.



No Sato the method that throws IOException will not cause any error. Because IOException is all so a subclass of Exception. If u execute your code then an error may come because you did not import java.io.*; to use the IOException.
Ingeneral if the base class method throws an exception then the derived class method that overrides this method can either throw the same exception or subclass of the same exception or may not even throw an exception.
Vaibhav Choudhary
Greenhorn

Joined: Apr 10, 2006
Posts: 5
Ya, 4th is right becasue its all of the way options is written,

3 is right but not necessary, one can implement the class without throwing any error.
So, you dont "have to" throw an exception, if you remove this "has to" from 3rd one, it is the most correct statement, i guess.

and as 4th is right, we can write without exception.
Hari Prasad Rao
Greenhorn

Joined: Apr 11, 2006
Posts: 3
U can jhave both 3 and 4 as legal answers. Legal in the sense u will not get compiler erros
Arno Reper
Ranch Hand

Joined: Mar 14, 2006
Posts: 286
3 isn't good because you could throw a subclass of exception but you don't have to!! only 4 is right...
arno


The man who makes no mistakes does not usually make anything<br /> <br />>>> SCJP 5.0 >> SCJD B&S <<< In progress
Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Hi,

Whenever we override a method from the superclass in the subclass there few conditions that must be satisfied and these are true for both abstract functions and non-abstarct functions:

1) Method name, its arguments, return type and access modifier can be same or less restrictive than the superclass function.

2) If the superclass function was throwing some exception then the subclass function at its will can or cannot throw the exception.

3) Subclasses are also allowed to throw a subclass of the exception declared in the superclass method.

4) Subclasses are also allowed to throw new unchecked exceptions.

Since Exception part of the method signature is optional for the subclass, so any option in which it is written "must or should have" will be wrong.

therefore, answer is 4.

Asha


SCJP 1.5
Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Hi,

Method name, its arguments, return type and access modifier can be same or less restrictive than the superclass function.


I meant to say method name, its argument types, and return type should be same
and the access modifier can be same or less restrictive than the superclass function.

Asha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can anyone explain me this?
 
Similar Threads
Exception - Abhilash Mock
Interface
Interface
Interface containing an abstract method.
class implementing interface