• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compile Time vs Run Time Assignments

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic