Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compile Time vs Run Time Assignments

 
Gerrod Bland
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After miserably failing to answer question 50 from Marcus Green's mock exam #2 I figure I'd make my first post and see if anyone knows of any good examples or exercises to help illustrate Marcus's definition. (Found here: http://www.jchq.net/tutorial/05_01Tut.htm )
Question 50:
Given the folowing classes which of the following will compile without error?

Any help would be much appreciated. Thanks in advance ...
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rules are pretty simple:
You can't instantiate an interface or abstract class.
You can store an instantiated object in a type that is:
a) itself
b) its parent
c) any interface implemented by itself
d) any interface implemented by its parent
Knowing that let's look at Marcus's example:

1 is an example of b - Object is the parent of CFace
2 is an example of b - Base is the parent of ObRef
3 is an error because you are storing an object in a variable that is its child type
4 is an example of b - Object is the parent of Base
Remember, Object is the parent of everything in Java.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic