• Post Reply Bookmark Topic Watch Topic
  • New Topic

compiling issue  RSS feed

 
denis bar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello , this question was in my test and I'm not sure about the response that I wrote , I'd love to get help.

Assuming that each of the following commands appears in the proper method defined
In a normal class , which ones are going through compiling ?

ArrayList<Circle> c1 = new ArrayList<Shape>();
ArrayList<Shape> s1 = new ArrayList<Circle>();
List<Shape> l1 = new ArrayList<Shape>();
ArrayList<Circle> c2 = new ArrayList<Circle>();
ArrayList<? extends Shape> s2 = new ArrayList<Circle>();
ArrayList<Shape> s3 = c2;
ArrayList<Circle> c3 = c2;
ArrayList<Object> o1 = new ArrayList<Object>();
List<Object> l2 = o1;
ArrayList<Object> o2 = new ArrayList<Circle>();

Thanks in advance
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about you start, by telling us your answers, and why for each?


And BTW, first, I am assuming the Shape class is a subclass of the Object class. And the Circle class is a subclass of the Shape class? Also, by "going through compiling", I am assuming you mean that the line is valid Java syntax (would have compiled)?

Henry
 
denis bar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's was my answer:

ArrayList<Shape> s1 = new ArrayList<Circle>();
List<Shape> l1 = new ArrayList<Shape>();
ArrayList<? extends Shape> s2 = new ArrayList<Circle>();
ArrayList<Object> o1 = new ArrayList<Object>();


and the answer to your question is yes
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
denis bar wrote:that's was my answer:


What do you mean by that? Are you saying that those are the only four lines that will compile? Or are you saying those are the lines that won't compile?

Henry
 
denis bar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I am saying that those are the only four lines that will compile(and I am not sure at all)
so, Henry can you help me with that please?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
denis bar wrote:Yes, I am saying that those are the only four lines that will compile(and I am not sure at all)
so, Henry can you help me with that please?


You do know that you can run each line through the compiler right? Adjusting for the lines that depends on other lines, of course. It definitely would have been quicker to figure it out yourself, than wait for an answer. Not to mention, being a better learning process.


Anyway, my answers would have been ... no, no, yes, yes, yes, no, yes, yes, yes, and no.

Henry
 
denis bar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm new at this . That's why I lacked confidence and knowledge .

Thank you.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WElcome to the Ranch

Not only should you find out by running the code which lines will and which will not produce a compiler error, but you should also work out why. The rules about what is and is not a subtype in Java® generics are complicated. Start with the Java® Tutorials and look for sections about generics. Use ctrl‑F‑“generics” because there are at least two generics sections.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!