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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:53:41+00:00 2026-05-29T03:53:41+00:00

I was reading Head First Java – pretty cool book to get started on

  • 0

I was reading “Head First Java” – pretty cool book to get started on Java. I have a question with regards to Generics.

The book had correctly stated that it is not possible to polymorphically pass a subtype to method accepting ArrayList. Example below –

public class Animal {

}

public class Dog extends Animal {

}

public class Cat extends Animal {

}

public class AnimalTester {
    public static void main (String[] args) {
      new AnimalTester().go(); 
    }


    public void go() {
      List<Animal> animalList=new ArrayList<Animal>(); 
      List<Dog> dogList=new ArrayList<Dog>();
      processAnimals(animalList); // this would compile
      processAnimals(dogList); // this would not compile
    }

    public void processAnimals(List<Animal> animalList) {

    }

}

However the above can be fixed by the use of generics –

public <T extends Animal> void processAnimals(List<T> animalList) {

}

But in the above case compiler would throw an exception if anything is tried to be added to the list (this is mentioned in the book). I tried it using my IDE and it did thrown an exception. The book had also mentioned that this is to ensure incorrect objects(e.g. cat) from being added to the list (dogList in our case).

However I am able to do this by casting the cat / dog / animal object using (T).

public <T extends Animal> void processAnimals(List<T> animalList) {
   animalList.add(new Animal()); // compilation Error
   animalList.add(new Dog()); // compilation Error
   animalList.add(new Cat()); // compilation Error
   animalList.add((T) new Cat()); // works and I can also cast it back 
                                  // to Cat and get a Cat object
}

As per the book, the whole reason why Java creators threw an exception is because incorrect object should not be added to the list. However is Java allowing it because the programmer is explicitly casting it and thereby saying “I know what I am doing”?

  • 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-29T03:53:42+00:00Added an answer on May 29, 2026 at 3:53 am

    Basically, you did an end run on the compiler.

    You have to think about the entire point of Generics. They are there for compile-time safety ONLY. They buy you nothing else. At runtime, due to type erasure, you’re still dealing with a List<Object>, which can hold anything. If you decided to explicitly cast something, then the compiler won’t second guess you. You’ve moved the potential danger from compile time to runtime, which is your choice.

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

Sidebar

Related Questions

A quote from the book I'm reading Head First Java : The point is
I'm reading Head First Design Patterns and have some understanding in Java. It starts
After reading the Head First Design Patterns book and using a number of other
I'm reading through head first design patterns at the moment and while the book
Just trying to get my head around Generics by reading this enlightening article by
I'm currently reading Head First's Object Oriented Analysis and Design. The book states that
After reading the most excellent book Head First Design Patterns, I began proselytizing to
I started learning rails a few days ago and I'm reading head first rails
i'm reading this book (Head First Object Oriented Design & Analysis). In chapter 5
I've been reading through Head First HTML5 Programming by O'Reilly. When I get to

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.