Method Signature in java
previous NextIn Java, a method signature is part of the method declaration. It's the combination of the method name and the parameter list(arguments). The reason for the emphasis on just the method name and parameter list is because of overloading. It's the ability to write methods that have the same name but accept different parameters. In other words, the signature of a method consists of the name of the method and the description (i.e., type, number, and position) of its parameters. called a method signature.
method signature example in java
class Signature {
int speed = 200;
public void start(int a){ //method signature
System.out.println("bike is speed = "+speed+"km/h");
}
public static void main(String[] args) {
Signaturea = new Signature();
a.start(5);
}
}
Output :
bike is speed = 200km/h
method signature example in java
class Method_Signature{
int a;
int n;
String s;
public void method1(int a){
this.a = a;
System.out.println("single parameters");
}
public void method2(String s,int n){
this.n = n;
this.s = s;
System.out.println("dubble parameters");
}
public void run(){
System.out.println(a +" "+ s+" "+ n);
}
public static void main(String[] args) {
Method_Signature a = new Method_Signature();
a.method1(5);
a.method2("signature",6);
a.run();
}
}
Output :
single parameters dubble parameters 5 signature 6
Method signature overloading in java
Method Signature Overloading in Java means having the same method name but a different parameter(arguments) name then we call them method overloading.
method overloading example in java
class Overloading{
int num = 2000;
String s = "Two thousand";
public void number(int a){
System.out.println("Number = "+num);
}
public void number(String a,int n){
System.out.println(s+""+num);
}
public static void main(String[] args) {
Overloading a = new Overloading();
a.number(5);
a.number("signature",6);
}
}
Output :
2000 Two thousand
Does the method signature include return type?
the method return type is not part of the method signature. Because in the method signature, we have to follow only method name and parameters (arguments).