Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how overriding works with generic types ?  RSS feed

 
gurpreet singh mahngar
Greenhorn
Posts: 3
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was going through java tutorials on genrerics at link https://docs.oracle.com/javase/tutorial/java/generics/bridgeMethods.html



how does overriding works here  ? can i say that setData() method of MyNode overrides Node<Integer> or not ?

further in the tutorial , it says after type erasure classes becomes as follows and a bridge method is generated for MyNode class. further as per the tutorial "After type erasure, the method signatures do not match. The Node method becomes setData(Object) and the MyNode method becomes setData(Integer). Therefore, the MyNode setData method does not override the Node setData method." so does it mean there is no overriding ?




Thanks Heaps

Cheers
 
Roel De Nijs
Sheriff
Posts: 11077
173
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpreet singh mahngar wrote:how does overriding works here  ? can i say that setData() method of MyNode overrides Node<Integer> or not ?

I think you can say that it's a valid override, because you can use the @Override annotation on setData(Integer) method and the code compiles successfully (illustrated in the following code snippet)If you use this annotation on a method which is not a (valid) override, you'll get a compiler error. If you try to compile the following code snippet, you'll get a compiler errorHere's the compiler error
Animal.java:8: error: method does not override or implement a method from a supertype
  @Override
  ^
1 error


And it gets really interesing when several generic types are involved. What do you think about the reset() methods of the subclasses of the Holder class?

 
gurpreet singh mahngar
Greenhorn
Posts: 3
Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it gets really interesing when several generic types are involved. What do you think about the reset() methods of the subclasses of the Holder class?


I will be honest that i ran this example by annotting the method with @Override annotation and it gave me compile time error wwhich means reset() in IntegerHolder/WordHolder is not a valid override of reset() in Parent class(Holder).

Now please correct me if am wrong , the reason would be :

It is not a valid override because reset(S t) method in IntegerHolder is generic whereas it extends Holder<Integer> which makes reset() in Holder<T> as reset (Integer t) and hence arguments do not match.
 
Roel De Nijs
Sheriff
Posts: 11077
173
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gurpreet singh mahngar wrote:It is not a valid override because reset(S t) method in IntegerHolder is generic whereas it extends Holder<Integer> which makes reset() in Holder<T> as reset (Integer t) and hence arguments do not match.

Indeed!

This would be a valid override of the reset() method defined by Holder<Integer>
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!