Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java generic method inheritance and override rules

I have an abstract class that has a generic method and I want to override the generic method by substituting specific types for the generic parameter. So in pseudo-code I have the following:

public abstract class GetAndParse {   public SomeClass var;    public abstract <T extends AnotherClass> void getAndParse(T... args); }  public class Implementor extends GetAndParse {   // some field declarations    // some method declarations    @Override   public <SpecificClass> void getAndParse(SpecificClass... args) {     // method body making use of args   } } 

But for some reason I'm not allowed to do this? Am I making some kind of syntax error or is this kind of inheritance and overriding not allowed? Specifically I'm getting an error about @Override because the eclipse IDE keeps reminding me to implement getAndParse.

Here's how I want the above code to work. Somewhere else in my code there is a method that expects instances of objects that implement GetAndParse which specifically means that they have a getAndParse method that I can use. When I call getAndParse on that instance the compiler checks to see whether I have used specific instances of T in the proper way, so in particular T should extend AnotherClass and it should be SpecificClass.

like image 827
David K. Avatar asked Mar 18 '11 00:03

David K.


People also ask

How do you override generic methods in Java?

A method in a generic class can be overridden like any other method. The output is shown here: The overridden version of getValue() is called for objects of type MyClass2, but the superclass version is called for objects of type MyClass.

Can generic be used with inheritance?

Generics also provide type safety (ensuring that an operation is being performed on the right type of data before executing that operation). Hierarchical classifications are allowed by Inheritance. Superclass is a class that is inherited. The subclass is a class that does inherit.

What are the rules to declare generic methods in Java?

The syntax for a generic method includes a list of type parameters, inside angle brackets, which appears before the method's return type. For static generic methods, the type parameter section must appear before the method's return type.

Can we override a method without inheritance?

If a method cannot be inherited, then it cannot be overridden. A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final. A subclass in a different package can only override the non-final methods declared public or protected.


2 Answers

What we are having here is two different methods with individual type parameters each.

public abstract <T extends AnotherClass> void getAndParse(Args... args); 

This is a method with a type parameter named T, and bounded by AnotherClass, meaning each subtype of AnotherClass is allowed as a type parameter.

public <SpecificClass> void getAndParse(Args... args) 

This is a method with a type parameter named SpecificClass, bounded by Object (meaning each type is allowed as a type parameter). Do you really want this?

Is the type parameter used inside Args? I think the problem would be there.


The meaning of

public abstract <T extends AnotherClass> void getAndParse(T... args); 

is that the caller of the method can decide with which type parameter he wants to call the method, as long as this is some subtype of AnotherClass. This means that in effect the method can be called with any objects of type AnotherClass.

Since the caller can decide the type parameter, you can't in a subclass narrow down the parameter type to SpecificClass - this would not be an implementation of the method, but another method with same name (overloading).

Maybe you want something like this:

public abstract class GetAndParse<T extends AnotherClass> {   public SomeClass var;    public abstract void getAndParse(T... args); }  public class Implementor extends GetAndParse<SpecificClass> {   // some field declarations    // some method declarations    @Override   public void getAndParse(SpecificClass... args) {     // method body making use of args   } } 

Now the getAndParse method implements the parent class' method.

like image 165
Paŭlo Ebermann Avatar answered Oct 21 '22 10:10

Paŭlo Ebermann


You are seeing this problem because of the concept called "Erasure" in Java Generics. Java uses "erasure" to support backward compatibility. i.e Java code which did not use generics.

Erasure Procedure:
The compiler will first do a type checking and then it will remove(erase) all the type parameters as much as possible, and also insert TypeCasting where ever necessary.

example:

public abstract <T extends AnotherClass> void getAndParse(T paramAnotherClass); 

will become

public abstract void getAndParse(AnotherClass paramAnotherClass); 

In class "Implementor.java",

The code

public <SpecificClass> void getAndParse(T paramAnotherClass) 

will become

public void getAndParse(SpecificClass paramAnotherClass){  } 

the compiler will see that you have not implemented the abstract method correctly. There is a type mismatch between the abstract method and the implemented method. This is why you are seeing the error.

More details can be found here. http://today.java.net/pub/a/today/2003/12/02/explorations.html

like image 36
kensen john Avatar answered Oct 21 '22 09:10

kensen john