This week's book giveaway is in the Spring forum. We're giving away four copies of Java Persistence with Spring Data and Hibernate and have Cătălin Tudose on-line! See this thread for details.
Select two methods that correctly override the following method? (Assume that all necessary packages have been imported).
protected int aMethod(int i) throws IOException {...}
A. int aMethod(int i) throws IOException{...} B. protected int aMethod(int i) throws FileNotFoundException{...} C. public int aMethod(int i) throws Exception{...} D. public int aMethod(byte i) throws IOException{...} E. protected int aMethod(int i) throws IOException,FileNotFoundException{...}
Plz some one explain whats the answer with clean explanantion
Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
Rules for override 1. method signature should be the same 2. argument list should be the same 3. return type should be the same 4. visibiliy cannot be reduced (public to private, protected to default etc) 5. cannot declare new exceptions as long as the new exception is a subclass of originally declared Exception.
A fails because of 4. C fails because of 5 D fails because of 2.
Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
c is wrong bec a overriding method must not throw broader checked exception. but it can throw narrower exception. "Exception" is the broadest exception so it cant be thrown.
regards anuj
SCJP 1.4, SCWCD 1.4
your eyes cannot see what your mind does not know
Because the overriding method can throw only those exceptions thrown by the overridden method or the subset of exceptions thrown by the overridden method. But in option C, the method is throwing Exception (the generic exception). This is contradicting our rule of exception for overriding. Thats why C is wrong.
Some legal overridden methods without exception. for example look at the following code
if the abstract method amethod() has default access modifier then you will override the following access modifier public/protected/deault but private is illegal. if the abstract method amethod() has protected access modifier then you will override the following access modifier public/protected/ but default & private is illegal. if the abstract method amethod() has public access modifier then all other access modifiers are illegal.
Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
1)the Stronger access restirction we can have is default for package level (assuming the overridding method is default)
2)(the Stronger access restirction we can have is protected for between two packages (assuming the overridding method is protected in different package)
I would like to add something in Rahul's answer : The method Cant not declare new 'Checked' Exceptions untill and unless the exceptions are subclass of the Declared Exception .But the overriden method can declare new 'Runtime Exception'ex. NullPointerException,ArithmaticException etc.
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!