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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:31:11+00:00 2026-05-31T06:31:11+00:00

I had a exam for a course named Java for Android I earlier today

  • 0

I had a exam for a course named “Java for Android I” earlier today and found this question that I just don’t get. Maybe some of you could explain it for me.

Question:

View this collection ArrayList p. A class Person is a
superclass to Student. ArrayList implements the List-interface
(according to the Java-API).

The compiler wont allow some of the situations noted in the code. Why?
Describe how they may entail that p wont contain instances of Person
or why p is not a ArrayList . E.g. p.add(“Hello”); now
contains a String instance.

Code:

p = new ArrayList<Object>();

void method ( ArrayList<Student> als) { ... }

void method ( List<Person> lp ) { ... }

void method ( ArrayList<Object> alo) { ... }

How would you describe this question? I just don’t get what my professor want’s me to answer.

Sorry if the quote has messed up grammar. I’ve been translating it from swedish.

  • 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-31T06:31:12+00:00Added an answer on May 31, 2026 at 6:31 am

    Your professor’s question is about the basics of type checking in Java generics. Assuming:

    class Person {
    }
    
    class Student extends Person {
    }
    

    This is a valid snippet:

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

    as is this:

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

    This is obviously not valid, because an object can be a Person instance without being a Student instance:

    List<Student> list = new ArrayList<Person>();
    

    But this is my favorite – it always trips people up:

    List<Person> list = new ArrayList<Student>();
    

    This is an invalid snippet. “What? Why!” I hear you ask. Well, it’s true that all Student objects are also instances of Person, so that assignments like this work:

    Person person = new Student();
    

    When it comes to parametric types, though, the Java compiler does its best to protect you. Imagine this piece of code:

    List<Person> list = new ArrayList<Student>();
    
    list.add(new Person());
    

    If the compiler did not object to the assignment, it would be now possible to enter Person objects to an ArrayList that only hosts Student objects…

    Method calls work in exactly the same way. Just strip the type of the argument from the method signature and try to imagine an assignment to that type…

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

Sidebar

Related Questions

I had an exam today and there was a question which I don't really
I'm studying for an exam, and a previous exam had this question: 1. void
Had a question about a implementation of bind function that I found on Mozilla's
I had this question during an exam, and I couldn't find a quick answer.
I have this question: suppose that the size of char is one byte and
Had no luck getting this to work via what I found on google.
alright so here is an excersise we had on today's exam of operating systems
I just finished taking a final exam on web applications. Capping off what had
Had a headace trying to get this to work, im trying to set up
Had a question that I've often wondered about. Is it better to have multiple

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.