-2

I have an exam coming up that I'm studying for.. and I was thinking of an elegant way to answer the following question, my current answer would be something like (based on information from this answer on stackoverflow)

The above initialization is not possible, it will compile but is missing an identifier in the declaration, and other functions such as add, remove will not be available.

The question..

Consider the following code snippet. Is it possible to assign an instance of ArrayList to a variable declared as Object as done in line 1? Explain.

  Object strings = new ArrayList();

  strings.toString();

What are other ways to answer this? and could you please provide wikipedia/resources, so I can research further into how the compiler would translate this into bytecode? Thank you

Community
  • 1
  • 1
classicjonesynz
  • 4,012
  • 5
  • 38
  • 78

2 Answers2

2

Consider the following code snippet. Is it possible to assign an instance of ArrayList to a variable declared as Object as done in line 1? Explain.

   Object strings = new ArrayList();

   strings.toString();

Yes it is possible. Object is a super type of ArrayList (in fact, Object is a super type of all Java reference types). It is legal to declare a variable to be of the super type of the actual type assigned.

A similar situation occurs when you define a List with the concrete implementation of ArrayList:

  List<String> list = new ArrayList<String>();
Greg Kopff
  • 15,945
  • 12
  • 55
  • 78
  • Thanks! :-) I'm going to do some more research on super types. :P – classicjonesynz Jun 05 '12 at 03:52
  • I would modify the answer slightly: *"in fact, Object is a super type of all Java **reference types**"*. A picky examiner might say that arrays are not classes ... which may or may not be true, depending on whose terminology you are using. Using the official Java term "reference type" removes any possible quibbles. – Stephen C Jun 05 '12 at 04:40
1

It does compile (did you try it?) but the resulting ArrayList is difficult to use because it is declared to be an Object. For example, the line:

strings.add("hello");

will not compile because add() is undefined for type Object. However,

((List)strings).add("hello");

does compile.

Jeremy Goodell
  • 18,225
  • 5
  • 35
  • 52
  • Thanks, yeah I've tested it can compile on eclipse but I had some research from [here](http://stackoverflow.com/questions/2516778/java-how-to-have-an-arraylist-as-instance-variable-of-an-object) and based on that question.. I thought it would compile, thank you for the clarification. – classicjonesynz Jun 05 '12 at 03:52
  • 1
    @Killrawr: the link post uses generics (the bit inside the < >) but doesn't have the variable name declared. Take a look at the last code block on my answer to see a correct generics example. (Your original question didn't use generics in the example code). – Greg Kopff Jun 05 '12 at 03:58
  • @GregKopff ohh good point. I thought defining lists of strings or datatypes instead of lists of objects. Would be good because objects retrieved from lists don’t have to be casted. – classicjonesynz Jun 05 '12 at 04:01