• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

method signature ?

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx to both of you ,


I have always depended on the kindness of strangers.

regards
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic