• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

doubt in method overloading..

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.*;
and handle o1.met(12,"ghani") with exception handler.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Yes with import added
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Ganesh,you are correct.
[ October 29, 2008: Message edited by: ramesh maredu ]
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot ramesh...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic