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

  • Home
  • SEARCH
  • 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 8309071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:59:50+00:00 2026-06-08T18:59:50+00:00

Consider the following Java class definitions: class Animal {} class Lion extends Animal {}

  • 0

Consider the following Java class definitions:

class Animal {}
class Lion extends Animal {}

When defining a covariant Cage for Animals I use this code in Java:

class Cage<T extends Animal> {
void add(T animal) { System.out.println("Adding animal..."); }
}

But the following Java example …

public static void main(String... args) {
    Cage<? extends Animal> animals = null;
    Cage<Lion> lions = null;
    animals = lions;         // Works!
    animals.add(new Lion()); // Error!
}

… fails to compile with the following error:

The method add(capture#2-of ? extends Animal)
in the type Cage
is not applicable to for the arguments (Lion)

Is this done because otherwise a different type like Tiger could be added after animals = lions and fail at runtime?

Could a special (hypothetical) rule be made that would not rejected it iff there would be only one sub-type of Animal?

(I know that I could replace add‘s T with Animal.)

  • 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-08T18:59:51+00:00Added an answer on June 8, 2026 at 6:59 pm

    In java :

    Cage<? extends Animal> animals = null;
    

    This is a cage, but you don’t know what kind of animals it accepts.

    animals = lions;         // Works!
    

    Ok, you add no opinion about what sort of cage animals was, so lion violates no expectation.

    animals.add(new Lion()); // Error!
    

    You don’t know what sort of cage animals is. In this particular case, it happens to be a cage for lions you put a lion in, fine, but the rule that would allow that would just allow putting any sort of animal into any cage. It is properly disallowed.

    In Scala :
    Cage[+T] : if B extends A, then a Cage[B] should be considered a Cage[A].

    Given that, animals = lions is allowed.

    But this is different from java, the type parameter is definitely Animal, not the wildcard ? extends Animal. You are allowed to put an animal in a Cage[Animal], a lion is an animal, so you can put a lion in a Cage[Animal] that could possibly be a Cage[Bird]. This is quite bad.

    Except that it is in fact not allowed (fortunately). Your code should not compile (if it compiled for you, you observed a compiler bug). A covariant generic parameter is not allowed to appear as an argument to a method. The reason being precisely that allowing it would allow putting lions in a bird cage. It T appears as +T in the definition of Cage, it cannot appears as an argument to method add.

    So both language disallow putting lions in birdcages.


    Regarding your updated questions.

    Is it done because otherwise a tiger could be added?

    Yes, this is of course the reason, the point of the type system is to make that impossible. Would that cause un runtime error? In all likelihood, it would at some point, but not at the moment you call add, as actual type of generic is not checked at run time (type erasure). But the type system usually rejects every program for which it cannot prove that (some kind of) errors will not happen, not just program where it can prove that they do happen.

    Could a special (hypothetical) rule be made that would not rejected it iff there would be only one sub-type of Animal?

    Maybe. Note that you still have two types of animals, namely Animal and Lion. So the important fact is that a Lion instance belongs to both types. On the other hand, an Animal instance does not belong to type Lion. animals.add(new Lion()) could be allowed (the cage is either a cage for any animals, or for lions only, both ok) , but animals.add(new Animal()) should not (as animals could be a cage for lions only).

    But anyway, it sounds like a very bad idea. The point of inheritance in object oriented system is that sometime later, someone else working somewhere else can add subtype, and that will not cause a correct system to become incorrect. In fact, the old code does not even need to be recompiled (maybe you do not have the source). With such a rule, that would not be true any more

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

Sidebar

Related Questions

Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>
Consider the following code public class sqldetails extends JFrame implements ActionListener{ static String username=null;
Consider the following Java function: public void foo(Class<? extends Exception> cl, List<? extends Exception>
Let's consider the following code in Java. package obj; final class First { public
Let's consider the following code snippet in Java. package escape; final public class Main
Consider the following Java exception classes: public class BarException extends RuntimeException { // [...]
Consider the following Java code: public class Test { private Foo< String, String >
Consider the following Java class: public class Foo { public static void doStuff() {
Consider the following classes in Java class A { protected void methodA() { System.out.println(methodA()
Please consider the following java source: package com.stackoverflow; public class CondSpeed { private static

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.