• 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
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

try this!

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
the following is a question from http://www.anilbachi.8m.com please tell me what is the solution!
Q].What is the result of compiling and running the following
class methodA(int i){
class test{
public void methodA(int i){
System.out.println(i);}
public int methodA(int i){
System.out.println(i+1);}
public static void main(String args[]){
test X=new test();
X.methodA(5);
}
}
}
a).The program compiles and prints 5
b).The program compiles and prints 6
c).the program gives a runtime exception because it doesnot find the method Test.methodA(int)
d).The program gives a compilation error because methodA is defined twice in class Test.
Thanks in advance
[This message has been edited by madhu kumar (edited February 06, 2001).]
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer is d. Return type is not part of the signature; therefore, the 2 functions can't be overloaded.
LHS
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say the answer is (d).
Because to do a valid overload, the signature of the overloaded function should be different. The return type may be same or may not be same. But just changing the return type, does not make a valid overload.
The code you have supplied seemed confusing. I asumed that the actual code is this:-
class test{
public void methodA(int i){
System.out.println(i);}
public int methodA(int i){
System.out.println(i+1);}
public static void main(String args[]){
test X=new test();
X.methodA(5);
}

Originally posted by madhu kumar:
Hi all,
the following is a question from http://www.anilbachi.8m.com please tell me what is the solution!
Q].What is the result of compiling and running the following
class methodA(int i){
class test{
public void methodA(int i){
System.out.println(i);}
public int methodA(int i){
System.out.println(i+1);}
public static void main(String args[]){
test X=new test();
X.methodA(5);
}
}
a).The program compiles and prints 5
b).The program compiles and prints 6
c).the program gives a runtime exception because it doesnot find the method Test.methodA(int)
d).The program gives a compilation error because methodA is defined twice in class Test.
Thanks in advance


 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there is also a missing '}'.
 
madhu kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi paul,
I have made changes to the code. There was a missing }
Thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic