• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object confusion

 
George Panadis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code (from one of the Ethnuware practice exam questions) will not run:


The compiler complaints that A is not compatible with AA.

Now, if we uncomment the a = null; statement, it compiles.
Why is this?
I am confused...
 
Paul Clapham
Sheriff
Posts: 21002
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't understand that either, until I tried to compile the code (both ways) and found that it compiled perfectly well (both ways).

However when I tried to run the code, it runs perfectly well because the compile was successful. In the one case the runtime (not the compiler) outputs that error message you posted, and in the other case it didn't output anything.

So you're confusing compiling code with running code. Hope that clears up the confusion.
 
George Panadis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I am using RAD so I guess I couldn't distinguish compile error vs runtime error.
Still, why doesnt it produce a runtime error when a = null ?
 
Paul Clapham
Sheriff
Posts: 21002
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because there's nothing wrong with assigning null to a variable of type AA.
 
Paul Clapham
Sheriff
Posts: 21002
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, in RAD compiler errors appear as red-squiggle underlines under the incorrect code elements.
 
George Panadis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. Thank you.
 
George Panadis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually there is one more thing that I could use clarification on.
Given the following code:


The previous code will not compile if I remove the AA cast from the last line.
Why does 'a' need to be cast to AA. It is already of type AA.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
George Panadis wrote:Why does 'a' need to be cast to AA. It is already of type AA.


But the compiler doesn't know that. All the compiler knows about is the reference type, and that is A because of how you declare the a variable on line 4.
 
George Panadis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
George Panadis wrote:Why does 'a' need to be cast to AA. It is already of type AA.


But the compiler doesn't know that. All the compiler knows about is the reference type, and that is A because of how you declare the a variable on line 4.


I see. Its starting to make sense. I should look more into what the compiler sees versus what is seen at runtime.
 
Paul Clapham
Sheriff
Posts: 21002
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's right. And remember that the compiler doesn't make any attempt to analyze the code (as you did there). All it officially knows about the variable "a" is that it was declared as type "A". It doesn't look at anything other than that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic