• Post Reply Bookmark Topic Watch Topic
  • New Topic

method signature ?  RSS feed

 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Does the meaning of method signature is different with respect to Overloading an Overriding.

As per the books , method signature includes method's name ,the number and type of arguments passed to it. The return type have no role in the signature.(Correct me if i am wrong)

For eg. Consider this Code

In Overloading

class T
{
public void goo()
{
System.out.println("true");
}
public int goo()
{
System.out.println("false");
return 1;
}
}

class OverLoad
{
public static void main(String arg[])
{
T ob = new T();
ob.goo();
}
}

this wont compile the compiler says

goo() is already defined in T


----------------------------------------------------------------
But In Over-ridding
class T
{
public void goo()
{
System.out.println("true");
}

}
class OverLoad extends T
{
public int goo()
{
System.out.println("true");
return 1;
}
public static void main(String arg[])
{
T ob = new OverLoad();
ob.goo();
}
}

if i change the return type here , the compile complains

goo() in OverLoad cannot override goo() in T; attempting to use incompatible return type
found : int
required: void
public int goo()


it seems that return type has some role in the method signature during overridding,otherwise it wont have given the error.


thanx in advance
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the rules prior to 1.5 of overriding was that you couldn't change the return of the method you override.

In 1.5 this has been changed to allow you to change the return type to a subclass of the original return type.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To determine whether this is overriding or overloading, the compiler looks only at the signature. The return type is not part of the signature, and so it has nothing to do with whether this is overriding or overloading. However once the compiler determines that you're attempting to do overriding, the return type becomes relevant to the question of whether or not this is a legal override. So the return type has something to do with this, but it's a little bit later in the process, and not part of the signature.
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx to both of you ,


I have always depended on the kindness of strangers.

regards
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!