What is a method signature anyone?

C

curious user

I've tried asking jeeves.

Is it the pattern of formal parameters for a method?
 
V

VisionSet

curious user said:
I've tried asking jeeves.

Is it the pattern of formal parameters for a method?

It is everything in the method declaration req'd to override an exisiting
method.

For instance in a super class

private void myMethod(String foo, int bar) {}

For a subclass to override myMethod it must declare it as:

x void myMethod(String myStringName, int myIntName) {}

where x is a visibility modifier that is equal to the superclasses or more
public, so in this case it can be anything, but if the superclass method was
public then it could only be public.

If you did not obey this and still used the method name myMethod, then the
compiler would complain.

If the parameters were different or ordered differently, eg (int myInt,
String myString)
Then this would be treated as a different method, this is legal, but would
not be overriding the original. Hence this is still part of the signature.
 
D

delusion

VisionSet said:
It is everything in the method declaration req'd to override an exisiting
method.

For instance in a super class

private void myMethod(String foo, int bar) {}

For a subclass to override myMethod it must declare it as:

x void myMethod(String myStringName, int myIntName) {}

where x is a visibility modifier that is equal to the superclasses or more
public, so in this case it can be anything, but if the superclass method was
public then it could only be public.

If you did not obey this and still used the method name myMethod, then the
compiler would complain.

If the parameters were different or ordered differently, eg (int myInt,
String myString)
Then this would be treated as a different method, this is legal, but would
not be overriding the original. Hence this is still part of the signature.

thankyou
 
D

Digital Puer

Roedy Green said:
yes. It is also a shorthand that Java uses internally in the class
file to describe it. See http://mindprod.com/jgloss/signature.html



Your definition says:
"Java has a way of compactly encoding the types of each parameter and
the return type on a given method. This string is called the
method's signature."

This is different from C and C++, right? In those languages, a
signature does not include the return type.
 
P

Peter Gal

curious said:
I've tried asking jeeves.

Is it the pattern of formal parameters for a method?

Java Language Spec (2nd edition) 8.4.2 Method Signature:
The signature of a method consists of the name of the method and the
number and types of formal parameters to the methdod. A class may not
declare two methods with the same signature, or a compile-time error occurs:

abstract class Point
{
int x, y;
abstract void move(int dx, int dy);
void move(int dx, int dy) { x += dx; y += dy; } // error
}

causes a compile-time error "move(int,int) is already defined in Point"

Peter G.
 
R

Roedy Green

This is different from C and C++, right? In those languages, a
signature does not include the return type.

Obviously C++ compilers have to be aware of return types when dealing
with methods, no different from Java. Both languages allow no
mechanism to have two different methods differing only in return type.

If there is a difference, it is just a terminology
convention. Java folk include the return type when they mean
signature. You are telling me C++ folk mean just the parameters,
probably because that is the only part relevant in matching up which
method to use.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,249
Members
46,845
Latest member
GygaKnight

Latest Threads

Top