Java generic method inheritance and override rules

java override method with generic return type
override method with generic parameters java
java override generic interface method
java generic method return type
override generic method c
java generics
java override method with subclass parameter
generic method in non generic class java

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.

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.

All About Overriding in Java, as defined in the parent class. Method overriding is done to achieve runtime polymorphism. AbstractDog class or the Animal interface, then it must override all the inherited. abstract methods, as subject to the normal Java access rules.​*/ A Closer Look at the Java Generic Factory Pattern · GraphQL  The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides.

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

12 Rules of Overriding in Java You Should Know, Rule #1:Only inherited methods can be overridden. Because overriding happens when a subclass re-implements a method inherited from a  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.

No, it's not valid. What would happen if someone with a GetAndParse reference called it with a different class extending AnotherClass?

Generic Methods (The Java™ Tutorials > Learning the Java , Static and non-static generic methods are allowed, as well as generic class constructors. The syntax for a generic method includes a list of type parameters, inside  I want to override equals() method in this class. I follow the usual rules while overriding the equals() method but and I typecast the Object into my class type. but in my equals() method, I want to return true only if the objects are of the same generic type. How can I check the run time type of the instance in my equals() method? Here is my code:

That becomes a nonsense when someone has a reference to type GetAndParse and tries to call the getAndParse method. If Cat and Dog extend AnotherClass. I should expect to be able to call GetAndParse#getAndParse with either a Cat or a Dog. But the implementation has tried to restrict it and make it less compatible!

Generics, Inheritance, and Subtypes (The Java™ Tutorials , Why Use Generics? Generic Types · Raw Types · Generic Methods · Bounded Type Parameters · Generic Methods and Bounded Type Parameters. Generics,  Recall that you cannot just shove objects into a collection of unknown type. The way to do deal with these problems is to use generic methods. Just like type declarations, method declarations can be generic—that is, parameterized by one or more type parameters. static <T> void fromArrayToCollection (T [] a, Collection<T> c) { for (T o : a) { c.add (o); // Correct } }

You cannot override to specific type T because there is in fact (at the bytecode level if you wish) only one method getAndParse because of type erasure (see other answer):

public abstract void getAndParse(AnotherClass... args); // (1)

For every type of T, the same method is used.

You can overload it (I think):

public void getAndParse(SpecificClass... args); // (2)

but this will not a different method from (1) ant it will not be called by generic code:

T x = whatever;
object.getAndParse(x); // Calls (1) even if T is derived from SpecificClass

Question about overriding of generic methods (Java in General , Question about overriding of generic methods. Post by: Gesu Ah, found the rule related to this that might help everyone JLS 8.4.2, which  You can't override a generic method's type parameter in a derived class. To achieve a similar functionality, one option is to have your base class be a generic class, and have your derived class such as where BaseClass is declared as

Overriding methods by passing as argument the subclass object , Based on the subtyping rules of a language like Java, if S is a subtype of T , then Your suggestion to accept that an overriding a method uses arguments that are And using generic covariance and contravariance you can relax the rules in  The overloading and overriding rule in Java Since you can either overload or override methods in Java, its important to know what are the rules of overloading and overriding in Java. any overloaded method or overridden method must follow rules of method overloading and method overriding to avoid compile time error and logical runtime errors; where you intend to override a method but method get overloaded.

Java - Use polymorphism or bounded type parameters, Java's poor implementation of generics (List<T> has different variance rules to There are two ways that adding a bounded generic type to a method as in concrete, non-inherited subclass methods for an inherited member variable. dog) { this.pet = dog; } @Override public void rewardPet() { // ---- Note this call ---- pet. Generics, Inheritance, and Subtypes As you already know, it is possible to assign an object of one type to an object of another type provided that the types are compatible. For example, you can assign an Integer to an Object , since Object is one of Integer 's supertypes:

Programming With - AngelikaLanger.com, This is a general rule in Java 5.0: if your source code compiled without any warnings Sometimes, when you believe you override a method inherited from a 

Comments
  • pseudo code too abstract, need more info
  • Using public abstract <T extends AnotherClass> void getAndParse(Args... args); makes no sense. What is the type parameter good for? How should the compiler determine its actual value and where should it use it?
  • Could you get us a complete example, which is in fact compilable (apart from the problematic error) and produces the error you mentioned?
  • @maaartinus: You're right I've changed the methods to make specific use of the generic type.
  • I don't want a type parameter. I want the compiler to check and make sure that SpecificClass is an extension of AnotherClass.
  • I don't understand why this is a problem. I have an implementor that extends GetAndParse and fixes the type of T to SpecficClass which the compiler checks to make sure extends AnotherClass then in my code when I have an instance of Implementor and call getAndParse with a type that doesn't match SpecificClass I should get an error.
  • @davidk01: The contract provided by GetAndParse say that everyone implementing it must do so for any type extending AnotherClass. Given GetAndParse foo = new Implementor(); you must able to call foo.getAndParse for any extension of AnotherClass - If the compiler didn't enforce this, you could just as well use Object, the point of generics is to ensure compile-time type safety
  • That is the reason that what you want to do is impossible, I apologize if I framed it in a way you don't like. Compile time generics are not capable of enforcing that for you. How is the compiler to know what subclass will be there at runtime? It's entirely possible the implementor is returned by a factory interface and comes from a different library you're not even compiling against! Narrowing of the type parameter cannot be checked for safety at compile time.
  • Can you be more descriptive by what you mean with your answer? I could guess that you're listing ways where Override doesn't work. But can you give an introduction why this has anything to do with this question? Please edit your answer to make it better.