This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in method overloading.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in method overloading.." Watch "doubt in method overloading.." New topic
Author

doubt in method overloading..

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
class Overl1
{
public void met(String s) ----------------------------line 1
{
System.out.println(s);
}
}
class Overl2 extends Overl1
{
public void met(int i,String s) throws IOException ----line 2
{
System.out.println(i);
System.out.println(s);
}
}

public class TestOverl
{
public static void main(String[] a)
{
Overl2 o1=new Overl2();
o1.met(12,"ghani");
}
}


my doubt is that when we are overloading the methods we can declare new or broader exceptions for the overloaded method.

here method (line 1) is overloaded by the method in line 2 which throws an exception.according to the statement Overloaded methods CAN declare new or broader checked exceptions which is given in K&B page no:105.but here it is giving compilation error.

why?
can anyone clarify me?


SCJP5 and SCWCD1.5
Think Twice Act Wise...
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

It gives error because o1.met(12,"ghani"); has to be enclosed in try catch or main method should include throws clause to declare the exception.The error you are getting is not related to overloading


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
subhasish nag
Ranch Hand

Joined: Apr 25, 2008
Posts: 101
import java.io.*;
and handle o1.met(12,"ghani") with exception handler.


Thanks,<br />Subhasish
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
yes ramesh i think you are right but in K&B book :


EXAM WATCH

Be careful to recognize when a method is overloaded rather than
overridden. You might see a method that appears to be violating a rule for overriding, but
that is actually a legal overload, as follows:
public class Foo {
public void doStuff(int y, String s) { }
public void moreThings(int x) { }
}
class Bar extends Foo {
public void doStuff(int y, long s) throws IOException { }
}
It's tempting to see the IOException as the problem, because the
overridden doStuff() method doesn�t declare an exception, and IOException is checked
by the compiler. But the doStuff() method is not overridden! Subclass Bar overloads the
doStuff() method, by varying the argument list, so the IOException is fine.

what about the last sentence?
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210



Above code just talks about overloading above code will compile even though we are throwing IOException.This is what they mean they are not talking about how to call the method.
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
but it is giving me compilation error if i am calling method in line-2.

you mean if i am not calling that method it compiles fine.
[ October 29, 2008: Message edited by: Ganeshkumar cheekati ]
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
What is the compile time error? What is the message?
Because it sure looks to me like that overloaded method needs to be in atry-catch block because, also from the K&B, if a method is called that declares throwing a checked exception, then it must either be caught or declared as thrown by the calling method.


------------------------
Bob
SCJP - 86% - June 11, 2009
qiurong zeng
Greenhorn

Joined: Oct 29, 2008
Posts: 4
Originally posted by subhasish nag:
import java.io.*;
and handle o1.met(12,"ghani") with exception handler.


Yes,this answer is right.
I add the follow code,and it is passed by the complier:
import java.io.*;

try{
o1.met(12,"ghani");
}catch(IOException ioe){
System.out.println("an IOException occured");
}
qiurong zeng
Greenhorn

Joined: Oct 29, 2008
Posts: 4
and another word,the complier give me a clear hint.
First the hint is:
over.java:11: 找不到符号(cann't find the symbol)
符号(symbol): 类(class) IOException
位置(location): 类(class) Overl2
public void met(int i,String s) throws IOException
^

after i add the import statement and complier it,the hint is :
E:\Java\IWSC> javac over.java
over.java:25: 未报告的异常 java.io.IOException;必须对其进行捕捉或声明以便抛出
(unreported exception ,you must catch it ��)
o1.met(12,"ghani");
^
1 错误(error)
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

you mean if i am not calling that method it compiles fine.


Yes with import added
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
yes ramesh.if i am not calling that method which throws exception it compiles fine.

if i am calling that i should declare or handle the exception.

is it ok?
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

Yes Ganesh,you are correct.
[ October 29, 2008: Message edited by: ramesh maredu ]
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
thanks a lot ramesh...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: doubt in method overloading..