Shababb Karim
Shababb Karim

Reputation: 3733

Java Method Signatures and Interface

We know that method signatures include only method name and parameter lists but not method return types. So why am I getting compiler error for the following code since java does not differentiate between methods with same signature.

 public class InterfaceTest implements I2{

     public void hello(){  }

     public void world(){  }
}

interface I1{
    public int hello();
}

interface I2 extends I1{
  public void world();
}

Upvotes: 3

Views: 8698

Answers (6)

Jordi Castilla
Jordi Castilla

Reputation: 26961

You have not overloading here, you are overriding and also hidding methods but not in a correct way.... there are 2 possibilities to solve your problem:

public class InterfaceTest implements I2{

    public void hello(int a){  }  // overloaded method

    @Override
    public int hello(){ return 1; }  // overriden method

    public void world(){  }   // this hides I1 method
}

Point is if you try this in a single class:

public void hello() {}
public int hello() {return 1;}

You will get Duplicate method hello() in type YourClass error, because to overloading a method you must change the FormalParameterListopt of the signature:

If two methods of a class [...] have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded.

Last but not Least point:

method signatures include only method name and parameter lists but not method return types

According JSL §8.4, when you declare a method:

MethodDeclaration:
    MethodHeader MethodBody

MethodHeader:
     MethodModifiersopt TypeParametersopt Result MethodDeclarator Throwsopt

MethodDeclarator:
    Identifier ( FormalParameterListopt )

So when you do this:

  public int hellow(int number) throws Exception;
//|      |   |      |           └ throwing an exception (Throwsopt)
//|      |   |      └──────────── receiving one int argument (MethodDeclarator FormalParameterListopt )
//|      |   └─────────────────── name hellow (MethodDeclarator Identifier)
//|      └─────────────────────── returning an int (Result)
//└────────────────────────────── is a public method (MethodModifiersopt)

Upvotes: 4

Nitesh Virani
Nitesh Virani

Reputation: 1712

You are referring method overriding here not overloading. The return type must be the same as, or a subtype, of the return type declared in the original overridden method in the superclass/interface.

Upvotes: 2

udyan
udyan

Reputation: 141

You are implementing an interface in a class, which have the same name of method as the interface "hello()", so there are two case

1) If you are implementing a interface you should write concrete implementation of its methods in this case

  1. public int hello();
  2. public void world();

but you provided implementation of only one method

  • "public void world()"

    so it will ask you to implement the other method which which will become part of this class.

2)When you provide the concrete implementation of the public void world() method it will collide with your method public void hello(), now as the overridden method become a member of your class and your method have same parameter list so it will fails the condition of the overloading.

so this will fail both the overriding and overloading functionality.

Upvotes: 1

codebox
codebox

Reputation: 20254

The return type of a method is part of its signature, your compile error is because your return type doesn't match the one declared in your intereface.

Upvotes: 1

Codebender
Codebender

Reputation: 14471

The problem is exactly because java cannot differentiate between methods with same signature (but different return types).

Let's see what happens in your case (assuming it was possible),

You can do the following,

InterfaceTest obj = new InterfaceTest();
obj.hellow(); // The compiler knows it returns void here.
i1 obj1 = new InterfaceTest();
obj1.hellow(); // The compiler thinks it return an int, when it doesn't return anything.

Upvotes: 1

Abbel
Abbel

Reputation: 320

According to this article the return-type is part of the declaration. And the declaration has to be the same in the implementing class. Also an extending interface inherits all the methods from the parent-interface so your hellow-method must match the hellow-method from i1

Upvotes: 1

Related Questions