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 9026625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:34:04+00:00 2026-06-16T06:34:04+00:00

It is about generics methods in java with two generics types (one for return

  • 0

It is about generics methods in java with two generics types (one for return type and another for a formal parameter) and how to implement it.
I guess i am missing something in the picture to get it work.

The matter is this…

This is working :

public enum Getter
{
    BillItemsSize {
        @Override
        public Integer get (Object entity) {
            DesiredClass ref = (DesiredClass ) entity; // "Old time" cast
            // do things...
        }
    };

    public abstract <T,K> T get (K entity);        
}

This is not working :

public enum Getter
{
    BillItemsSize {
        @Override
        public Integer get (DesiredClass entity) { // no cast at all
            // do things
        }
    };

    public abstract <T,K> T get (K entity);        
}

The java compiler yells me this :

<anonymous datasource.db.Getter$1> is not abstract and does not override abstract method <T,K>get(K) in Getter

Well, that’s the situation.
Thanks in advance to all!.
Hope it helps others in the future!.

P.D: It’s not a problem of enums types.. it happens across classes hierarchies. So don’t bother blaming the enums, i tried this and doesn’t work.

public abstract class SuperClass
{
    public abstract <T,E> T pepe (E e);
}

public class SubClass extends SuperClass
{

    @Override
    public Integer pepe(DesiredClass e) // fails... 
    {
        return null;
    }
    
}

UPDATED:

For Generics parameters

We can make a general rule statement that for "generics parameter" (those whose types is generic) the type implicitly taken in the method signature is equals to upper limits of that generic, that can be Object, if nothing is specified, or a more specific subclass if upper bounds are used (in example T extends String).

For Generics returns types

There is no problem overwriting a generic method with a specific return type, as long the return type is a subtype of the overwritten return type. And what is the return type at first place? Well, it happens to be Object. By default the compiler assumes (in the method signature) the generic type as an Object type.

So we have to thing that the trick is to know that any method that have a generic return type are actually having an Object return type.
Then if in any subclasses the method is overwritten and we alter he return type stating that return another type, there will be no problem. Because, besides that method will return an object of another class, that object returned will inevitable be subclass of the Object class and there will be no problem overwrite a method with a different return type that the original as long it be a subtype of the original. The technique called covariant return type allow us to do such thing.

public abstract class SuperClass
{
    public abstract <T> T operation ();
}


public class SubClass extends SuperClass
{

    @Override
    public Chair operation()
    {
        //bla bla 
    }
    
}   

meanwhile in another part of the code…

void main ()
{
        SubClass sb = new SubClass();
        Chair chair = sb.operation ();
        // the chair type can be easely replaced by super type (like Object)
        Object object = sb.operation();
}

Thanks to all the folks who help clear this out!.

  • 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-16T06:34:05+00:00Added an answer on June 16, 2026 at 6:34 am
    public abstract <T,K> T get (K entity);        
    

    is a method that can take anything as argument, and is allowed to return anything.

    Overriding it with

    public Integer get (DesiredClass entity)
    

    doesn’t work, since you restrict the types of argument that can be passed to the method to DesiredClass, and thus break the Liskov principle.

    It would be easier to understand without generics. Suppose you have an abstract method in class Bar:

    public abstract void fillRecipient(Recipient r);
    

    and you try to override in SubBar it with

    public void fillRecipient(Glass glass) {
    }
    

    What would the following code do if the above was legal?

    Bar bar = new SubBar();
    bar.fillRecipient(new Mug());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly I ve searched about usage of Generic Types in java, however answers I
I'm a bit confused about how Java generics handle inheritance / polymorphism. Assume the
There are already some discussions here on stackoverflow about Java generics, but I am
I have a problem about sending sms in android. I have two classes. one
I have a Assignment question about Java Generics. The given class is Product.java. It
I am a bit of confused about java generics Here is the code class
I have a Java question about generics. I declared a generic list: List<? extends
I've always been wondering about some weird aspect of Java generics and the use
I have a basic question about generics in Java. I have a class X
Quick question about syntax in Java. I want to take a Set and return

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.