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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:17:55+00:00 2026-05-25T20:17:55+00:00

My question is about generics in Java 7. Suppose we have such class hierarchy:

  • 0

My question is about generics in Java 7. Suppose we have such class hierarchy:

interface Animal {}    
class Lion implements Animal {}    
class Butterfly implements Animal {}

Just like in Java Generics Tutorial

Also we have a class

class Cage<T> {
    private List<T> arr = new ArrayList<>();
    public void add(T t) {
        arr.add(t);
    }
    public T get() {
        return arr.get(0);
    }
}

And here is the code which uses that classes:

public static void main(String[] args) {
        Cage<? extends Animal> cage = new Cage<>();
        Animal a = cage.get(); //OK
        cage.add(new Lion()); //Compile-time error
        cage.add(new Butterfly()); //Compile-time error   
    }

Question #1:

I have read here about these issues but there was simply like Cage<?>. But I tell the compiler <? extends Animal> so type T in Cage<T> will be any of subtypes of Animal type. So why it still gives a compile time error?

Question #2:

If I specify Cage<? super Animal> cage = ... instead of Cage<? extends Animal> cage = ... everything works fine and compiler doesn’t say anything bad. Why in this case it works fine while in the example above it fails?

  • 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-05-25T20:17:56+00:00Added an answer on May 25, 2026 at 8:17 pm

    The cage must be able to hold both types of animals. “super” says that – it says that the Cage must be able to hold all types of animals – and maybe some other things, too, because ? super Animal might be a superclass of Animal. “extends” says that it can hold some kinds of animals – maybe just Lions, for instance, as in:

    Cage<? extends Animal> cage = new Cage<Lion>();
    

    which would be a valid statement, but obviously the lion cage won’t hold butterflies, so

    cage.add(new Butterfly());   
    

    wouldn’t compile. The statement

    cage.add(new Lion());
    

    wouldn’t compile either, because Java here is looking at the declaration of the cage – Cage<? extends Animal> – not the object that’s assigned to it right now (Cage<Lion>).

    The best description of generics I know of is in O’Reilly’s Java in a Nutshell. The chapter is free online – part 1 and part 2.

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

Sidebar

Related Questions

I have a question about Java Generics. If I define a class like this:
A question just came up about java generics. The example code is: public interface
I have a Assignment question about Java Generics. The given class is Product.java. It
I have a question about Java Generics and Collections. It's considered good practice to
I have a Java question about generics. I declared a generic list: List<? extends
I'd have a question here considering java generics. I have a generic class called
Just got a question about generics, why doesn't this compile when using a generic
No, this is not a question about generics. I have a Factory pattern with
I have a question about GUI design, specifically with Java Swing and creating clean
I have this question about best practices in following examples: interface Request; interface Service

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.