Core Java

Java Method Signature

In this post, we are going to talk about the Java method signature, method overloading, and method overriding by examples. The JDK version we use to compile the source code in this example is OpenJDK 13 and the IDE we use is Eclipse IDE 2020-03.

1. What is a method signature in Java

In Java programming language, the method signature consists of two parts: the method’s name and the parameter list. These two parts are part of a method declaration. The parameter list includes the number, type, and order of parameters but not the name of the parameter. The name of the parameter is ignored by the compiler for checking method uniqueness. Return types and exception lists are not part of the method signature.

For example, the following four methods are considered to be the same:

public void hello(String name)
protected String hello(String firstName) throws Exception
CharSequence hello(String character)
int hello(String lastName)

This is because they have the same method signature hello(String).

However, the following two methods have distinct signatures and are considered to be different methods:

public String hello(String name)
public String hello(CharSequence name)

This is because the first method’s signature is hello(String) but the second method’s signature is hello(CharSeequence).

Note that both method names and parameter names are “identifiers” in Java, They share the same rules regarding valid names. You can have a look at §3.8 from the Java Language Specification to find out exactly what an identifier may contain. In addition, all keywords in Java are reserved and cannot be used as a method name or a parameter name. You can find all keywords in Java at §3.9 from the Java Language Specification.

2. Polymorphism

Polymorphism means “multiple forms”. In object-oriented programming languages like Java, polymorphism means we can use one single interface to represent various classes. There are two types of polymorphism in Java: compile-time and runtime. Method overloading and method overriding are two examples of polymorphism in Java.

2.1 Method Overloading

Method overloading is an example of compile-time polymorphism. It is also called “static binding”. It means methods declared in a class having the same method name but different parameter list.

In the example below, we can see that there are two methods that have the same method name “hello” but different parameters. As they have different method signatures, the Java compiler is able to differentiate them and invoke them by checking their method signatures. This is done by the compiler at compile-time, so we call it compile-time polymorphism or static binding.

MethodSignatureExample.java

public class MethodSignatureExample {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // constructs a MethodSignatureExample instance
        MethodSignatureExample example = new MethodSignatureExample();
        // calls the first method
        System.out.println(example.hello("Kevin"));
        // calls the second method
        System.out.println(example.hello("Kevin", 1));
    }

    /**
     * The first method's signature is "hello(String)".
     * 
     * @param name the first name
     * @return a hello string with name
     */
    public String hello(String name) {
        return "Hello, " + name + "!";
    }

    /**
     * The second method's signature is "hello(String, int)".
     * 
     * @param name the first name
     * @param mood 0 - sad face, 1 - happy face
     * @return a hello string with name and mood
     */
    public String hello(String name, int mood) {
        return "Hello, " + name + "! " + (mood == 0 ? ":(" : ":)");
    }
}

The output of the example is below:

Hello, Kevin!
Hello, Kevin! :)

2.2 Method Overriding

Method overriding is an example of runtime polymorphism. It is also called “dynamic binding”. It means the actual method to be called is not decided by the compiler at compile time but is determined at run time. When we invoke a method that has been overridden, the runtime will find out the actual method to be called. Comparing to static binding, dynamic binding is slower because the runtime needs time to figure it out correctly.

In the example below, we create a private static internal class OverrideExample by extending from class MethodSignatureExample. Then we override the method “hello(String)” in superclass MethodSignatureExample by adding ” Nice to meet you!” to the return value. So when we call hello("Kevin") on the variable overrideExample declared as class MethodSignatureExample, the runtime actually calls the overridden method hello(String) defined in class OverrideExample.

OverrideExample.java

public class MethodSignatureExample {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // constructs a MethodSignatureExample instance
        MethodSignatureExample overrideExample = new MethodSignatureExample();
        // calls the hello method of MethodSignatureExample
        System.out.println(overrideExample.hello("Kevin"));
        // constructs an OverrideExample instance
        overrideExample = new OverrideExample();
        // calls the hello method of OverrideExample
        System.out.println(overrideExample.hello("Kevin"));   
    }

    /**
     * The first method's signature is "hello(String)".
     * 
     * @param name the first name
     * @return a hello string with name
     */
    public String hello(String name) {
        return "Hello, " + name + "!";
    }

    private static class OverrideExample extends MethodSignatureExample {
        /**
         * Overrides the method "hello(String)" in superclass MethodSignatureExample by
         * adding " Nice to meet you!" to the return value.
         */
        @Override
        public String hello(String name) {
            return super.hello(name) + " Nice to meet you!";
        }
    }
}

The output of the example is below:

Hello, Kevin!
Hello, Kevin! Nice to meet you!

3. Download the Source Code

Download
You can download the full source code of this example here: Java Method Signature

Kevin Yang

A software design and development professional with seventeen years’ experience in the IT industry, especially with Java EE and .NET, I have worked for software companies, scientific research institutes and websites.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button