What is method signature in Java – Does it include return type?

What is method signature in java ? – Method signature in java includes only method name and parameters. Method return types are not included in method signature.

For example, in below class method declaration only “foo(String str)” is the method signature that contains method name and parameters. Return type String should in excluded.

class MyClass{
	
	public String foo(String str) {
		
		return str;		
	}	
}

NOTE:

Overloaded methods (multiple methods with same name) in a class are also differentiated by the method signatures. Overloading does not include return types.

In below class method examples , two overloaded method by different return types i.e. void and int. This is wrong and compiler will flash an error.

class MyClass{
	
	public void f() {	
			
	}	
	public int f() {
		
		return 0;
		
	}	
}

Overloaded methods should be only differentiated by method signatures. For example, below class has two overloaded methods by different method signatures i.e. f() and f(int a).

class MyClass{
	
	public void f() {	
			
	}	
	public int f(int a) {
		
		return 0;
		
	}	
}.

Also, note that in java programming, method declaration must contains return types. Method signatures does not include return type.

Related Posts