Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8578503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:24:16+00:00 2026-06-11T20:24:16+00:00

If I have a base class like this that I can’t change: public abstract

  • 0

If I have a base class like this that I can’t change:

public abstract class A {
    public abstract Object get(int i);
}

and I try to extend it with a class B like this:

public class B extends A{
    @Override
    public String get(int i){
        //impl
        return "SomeString";
    }
}

everything is OK. But my attempt to make it more generic fails if I try:

public class C extends A{
    @Override
    public <T extends Object> T get(int i){
        //impl
        return (T)someObj;
    }
}

I can’t think of any reason why this should be disallowed. In my understanding, the generic type T is bound to an Object—which is the requested return type of A. If I can put String or AnyObject as my return type inside B, why am I not allowed to put <T extends Object> T inside my C class?

Another strange behavior, from my point of view, is that an additional method like this:

public class D extends A{

    @Override
    public Object get(int i){
        //impl
    }

    public <T extends Object> T get(int i){
        //impl
    }
}

is also not allowed, with the hint of a DuplicateMethod provided. This one, at least, confuses me, and I think Java should make a decision: if it is the same return type, why not allow overriding; and if it is not, why shouldn’t I be able to add this method? To tell me it’s the same, but not allow it to be overridden, is very weird, based on common sense.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:24:18+00:00Added an answer on June 11, 2026 at 8:24 pm

    JLS # 8.4.2. Method Signature

    The signature of a method m1 is a subsignature of the signature of a method m2 if either:

    • m2 has the same signature as m1, or

    • the signature of m1 is the same as the erasure (§4.6) of the
      signature of m2.

    As per above rule as your parent do not have an erasure and your child has one so it is not a valid overriding.

    JLS#8.4.8.3. Requirements in Overriding and Hiding

    Example 8.4.8.3-4. Erasure Affects Overriding

    A class cannot have two member methods with the same name and type erasure:

    class C<T> {
        T id (T x) {...}
    }
    class D extends C<String> {
        Object id(Object x) {...}
    }
    

    This is illegal since D.id(Object) is a member of D, C.id(String) is declared in a supertype of D, and:

    • The two methods have the same name, id
    • C.id(String) is accessible to D
    • The signature of D.id(Object) is not a subsignature of that of
      C.id(String)
    • The two methods have the same erasure

    Two different methods of a class may not override methods with the same erasure:

     class C<T> {
         T id(T x) {...}
     }
     interface I<T> {
         T id(T x);
     }
     class D extends C<String> implements I<Integer> {
        public String  id(String x)  {...}
        public Integer id(Integer x) {...}
     }
    

    This is also illegal, since D.id(String) is a member of D, D.id(Integer) is declared in D, and:

    • The two methods have the same name, id
    • D.id(Integer) is accessible to D
    • The two methods have different signatures (and neither is a
      subsignature of the other)
    • D.id(String) overrides C.id(String) and D.id(Integer)
      overrides I.id(Integer) yet the two overridden methods have the same
      erasure

    Also It gives example of a case where it is allowed from super to child

    The notion of subsignature is designed to express a relationship between two methods whose signatures are not identical, but in which one may override the other. Specifically, it allows a method whose signature does not use generic types to override any generified version of that method. This is important so that library designers may freely generify methods independently of clients that define subclasses or subinterfaces of the library.

    Consider the example:

    class CollectionConverter {
    List toList(Collection c) {...}
    }
    class Overrider extends CollectionConverter {
     List toList(Collection c) {...}
    

    }

    Now, assume this code was written before the introduction of generics, and now the author of class CollectionConverter decides to generify the code, thus:

     class CollectionConverter {
       <T> List<T> toList(Collection<T> c) {...}
     }
    

    Without special dispensation, Overrider.toList would no longer override CollectionConverter.toList. Instead, the code would be illegal. This would significantly inhibit the use of generics, since library writers would hesitate to migrate existing code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine that I have a generic base class like this: public abstract class AnimalDTO<TA,
I have a base class like this: class FooBase { public bool Do(int p)
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
I have a base class like this: public class BaseResponse { public string ErrorMessage
I have an abstract method in a base class declared like so... public abstract
I have a base class that I want to look like this: class B
We have a legacy application that has a class like this : public class
I have a base class which looks something like this: class Base { public:
Create a function that can have different argument list. something like this void s(int
I have a base class that is a template that looks like this: template

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.