Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Override - Overload - quick question

 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

without using a compiler, will this compile:


If not, where is the problem and why?!

I am curious to read answers.
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think this will compile. Overloaded methods must differ in their arguments and not just the return type. First and third methods have the same set of arguments and only different in their return type.
 
m ali
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tommasso,

This wont compile. The compiler will not resolve which amethod is calling with the same arguements - int and String. Am I right?

Thanks.
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah,

you are both right.
The trick here is that the return type doesn't make a override!

Thanks.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey
Hey Tommaso,

Originally posted by Tommaso Nuccio,



without using a compiler, will this compile:

code:


class Rid{
public void amethod(int i, String s){}
public void amethod(String s, int i){}
public int amethod(int i, String s){}
}


See the question what you ask,
without using a compiler



cmbhatt
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tommaso Nuccio:
Yeah,

you are both right.
The trick here is that the return type doesn't make a override!

Thanks.


Override? There is nothing being overridden here. This is about overloading.
[ April 16, 2007: Message edited by: Barry Gaunt ]
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barry,

you are right.
Sorry, that was a typo.

Regards.
 
Gaurav Pavan Kumar Jain
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tommaso

Whenever you want to overload a method you must take care of parameters you are passing. All the parameter you are passing in overloading a method must be different in each method.


Best Of Luck
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic