0

In version 1.5, Java have introduced the concept of auto-boxing.

public interface SomeInterface { public void test(Integer val); } public class Main implements SomeInterface { /*The method signature gets different and compiler is asking to override un-implemented methods*/ public void test(int t) { } } 

Then why I am getting compile time error for overriding un-implemented methods, why above test method's arguments are not auto-boxed to match parent test method signature?

1
  • the feature that is demonstated here is called method overloading... but you need to override here Commented Jul 3, 2014 at 9:19

5 Answers 5

7

It's because the method in subclass is not override-equivalent with the one in super class. The super class method can take null as argument, while the subclass method can't (There's nothing about auto-boxing here).

Sign up to request clarification or add additional context in comments.

5 Comments

@WillieWheeler THIS has happened with me so many times... pssshhh.
Sir, if i replace Interface method with public void test(Number val); and implemented method with public void test(Integer t){} it is not working
@LMK Because again the subclass method is not override equivalent. Basically your argument type should be same in overridden method. You can't have covariant types in argument but only in return type.
@LMK So, parameter type should exactly be same. Try this with return type, and it will work.
Please add the comment that clarify your answer in it and copy the part of the link into the answer so (even if it might never happens) the link dies, the answer stay useful.
1

Because Integer Not Equal to int

  • Integer is class
  • int is primitive type

So both methods have different argument types that's why you are not overriding the method but creating newer one in your class.

You can call method with autoboxing feature but you can not ovverride.

Overriding

An instance method in a subclass with the same signature (name, plus the number and the TYPE of its parameters) and return type ....


The Java compiler applies autoboxing when a primitive value is:

  • Passed as a parameter to a method that expects an object of the corresponding wrapper class.
  • Assigned to a variable of the corresponding wrapper class.

Comments

0

Autoboxing and unboxing can happen anywhere where an object is expected and primitive type is available for example In method invocation where an object argument is expected, if you pass primitive, Java automatically converts primitive into equal value.

Read more: http://javarevisited.blogspot.com/2012/07/auto-boxing-and-unboxing-in-java-be.html#ixzz36Rhg91CB.

and here your class Main implements interface SomeInterface and this is the implementation of method not the calling of a method where autoboxing works.

Comments

0

This is conceptually impossible, because the Java runtime does not know a thing about boxing. The boxing feature was implemented on the Java compiler level, similarly to generics. As a matter of fact, the Java runtime does not consider a primitive int and a boxed Integer to have anything in common, even in the current Java 8 version.

Instead, whenever you make an assignment like:

Integer i = 42; 

the Java compiler desugars this expression to

Integer i = new Integer(42); 

which is then observed as above by the Java runtime. At the same time, the Java runtime considers

void foo(int i); void foo(Integer i); 

to be different methods which is why boxing would have been difficult to be implemented at the runtime level for reasons of backwards compatibility.

Comments

0

The simple answer to this is :

If 2 methods can be overloaded, it means, compiler considers the signatures to be different.

And, for Overriding a method, both the methods should be having the same signature.

E.g. :

//Method overloading, compiler doesn't complain, which means "signatures are different", //because one is expecting an object of Integer Class, and other is expecting a primitive value. class A{ public void a(Integer a){...} public void b(int a){...} } //Method Overriding, compiler complains, because "signatures are different!" class A{ public void a(Integer a){...} } class B extends A{ @Override public void a(int a){...} } 

7 Comments

Did not answer my question. I am having interface, and there won't be compile time error. If you are adding something like "Note" do provide the reference.
The NOTE was for the overloading example I provided, have edited my answer accordingly.
U can check the updated comment in Method Overloading example section.
thanks for updating, the whole point was if you are quoting something then you are supposed to provide a reference.
also whatever you posted, comes down to my initial question which is "why above test method's arguments are not auto-boxed to match parent test method signature" I am looking for why compiler does that not how compiler behaves :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.