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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:21:41+00:00 2026-06-11T17:21:41+00:00

In Kathy Sierra’s book on SCJP we learn that, When we write List <A>

  • 0

In Kathy Sierra’s book on SCJP we learn that, When we write

List <A> list = new ArrayList <A> ();

This means that this list will accept elements of type A only, not it’s subtype or supertype, only type A. Then I came across this example

import java.util.*;

interface A {
    public void a();
}
class B implements A {
    public void a() { }
    public void b() { }
}

class C extends B {
    public void a() { }
}

class D extends C {

}

public class Generics {
    public static void main() {
        List <B> lst = new ArrayList <B> ();
        lst.add(new B());
        lst.add(new C());
        lst.add(new D());
    }
}

Here a list has been declared with B as it’s bound type. But it looks like it’s accepting objects of it’s subtype also. Why is it so? And if it is possible then is this declaration available in java

List <? extends B> list = new ArrayList <B> ();

Now with this notation we tell compiler that any type which extends B is allowed to enter this list. Please help, I am really confused

  • 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-11T17:21:42+00:00Added an answer on June 11, 2026 at 5:21 pm

    The problem in teaching generics with the List examples is that one can understand the examples and still not the rule.

    With the advent of generics, the compiler hasn’t been instructed with human knowlege, so it doesn’t know that a List<A> is an ordered sequence of elements (it could be a proboscidean with long, curved tusks), and thus it can’t guarantee that a List will accept elements of type A only. All the compiler knows is supplied by the following definition

    interface List<E> {
      public void add(E);
      public E get(int index);
    }
    

    This code does not define a single type: it defines an infinite series of types:

    interface ListOfStrings {
      public void add(String s);
      public String get(int index);
    }
    
    interface ListOfShapes {
      public void add(Shape e);
      public Shape get(int index);
    }
    

    depending on how you instantiate the parameterized type. Since in Java each subtype is a valid replacement for its supertypes, a Rectangle is a legal argument to a List<Shape>‘s add(Shape s).

    Here is when one thinks “Generics, I finally got you!“. Then he fires an IDE, types

    List<Shape> shapes = new LinkedList<Rectangle>();
    

    and the compiler refuses to compile. “- What’s going on here? I can add a rect to a list of shapes, but a list of rects is not a list of shapes?”. The problem is one thinks in terms of lists, shapes and rectangles, but the compiler doesn’t know any of these. It sees

    T reference = <expression returning type S>
    

    so it wonders “- Is S a valid replacement for type T?”. Phrased differently, is T a parent of S in the type hierarchy, as constructed according to the Java rules? So it carefully checks its old Java book and finds that no, X<A>is not a X<B> parent. Illegal code. Stop.

    “- But a list of rectangles definitely contains shapes! I just compiled a program which added a bunch of rectangles to a List<Shape>!”. This doesn’t mean anything to a compiler, it’s not such a smart program – you see… It can only interpret those few rules it learned some years ago and can’t even tell the difference between a List and a Mammoth. It only knows its old Java book, and in the book is clearly stated and remarked that there is no subtyping relation between X<A> and X<B>, no matter how A and B relate to each other. “- If only generics were implemented like arrays… You, stupid Java people…” In fact:

    String[] strings = new String[10];
    Object[] objects = strings;
    objects[0] = new Rectangle(); // ArrayStoreException at runtime
    

    Maybe you now got the point. After all, those Java people are not so stupid… At least, those fancy rules they chosen for their type system serve a purpose, at least they did it for a practical reason. They made arrays covariant, but generics invariant. Since both arrays and lists’ contents can be changed (they are mutable), this exposes arrays (but not lists) to the problem seen above. For example, Scala achieves type safety by making lists covariant but immutable (a List[Integer] can be assigned to a List[Number], but they’re read-only) and arrays mutable but invariant (you can’t use an Array[Integer] where an Array[Number] is required, but you can modify their contents).

    Finally, to port the subtyping relation between A and B into the generics world, one can use the wildcard ?, but that’s matter for a whole new story. I just anticipate that in the old Java book is written that List<Rectangle> does not extends List<Shape>, but it’s a legitimate child of List<? extends Shape>.

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

Sidebar

Related Questions

I read in Kathy Sierra book that when we create String using new operator
I'm reading SCJP Java 6 by Kathy Sierra and Bert Bates and this book
I read this in Kathy Sierra's book: Local variables are sometimes called stack, temporary,
This question was taken from Kathy Sierra SCJP 1.6 . How many objects are
I am reading the SCJP book by Kathy Sierra. I find Polymorphism a little
In the book of SCJP guide by Kathy Sierra, in the assignments chapter, we
I'm reading SCJP by Kathy Sierra and Bert Bates and it says on pg.
I'm trying to write a simple php function that will strip everything between two
The question below is from Java SCJP5 book by Kathy Sierra and Bert Bates.
In this answer to a question I asked. Kathy Van Stone says that adding

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.