• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

implementing and extending

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answers are 1,2, and 4. I would think that #3 would be correct and #2 would be false.


[This message has been edited by Willie Toma (edited August 29, 2001).]
[This message has been edited by Willie Toma (edited August 29, 2001).]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Willie,
Let's go through the options:
1. o1 is a reference of type Object, o2 is a reference of type IFace pointing to an object of class CFace. Since CFace is implicitely an object the assignment is valid.
2. b is a reference of type Base, ob is a reference of type ObRef pointing to an object of class ObRef. Since ObRef is a subclass of Base the conversion in the assignment is allowed and thus the assignement is calid.
3. b and ob the same as in 2, but this time we try to have a conversion of b to ob, this is not valid since b (of class Base) is a superclass of ob (of class ObRef), thus a explicit cast is needed here otherwise there will be a compilation error.
4. o1 is a reference of type Object, b a reference of type Base pointing to an object of class Base, since Base is_a Object (i.e. Base is a subclass of Object) the assignment is valid.
Keep in mind the following
if you are trying to convert an object in an assignment like this
Oldtype o;
Newtype n = o;
then Oldtype must be a subclass of Newtype OR in the case Newtype is an interface, Oldtype must implement the interface Newtype.
Hopes that helps, let me know if something is wrong or unclear
Val
 
Willie Toma
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"You can assign up the inheritance tree from a child to a parent but not the other way without an explicit casting."
I think I'm confusing the parent and the child. 'ObRef' extends 'Base', doesn't that make Base the parent?
 
Willie Toma
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As is
o1 = ob // would be considered true
If this was written in the block of code:
IFace iF = new IFace();
CFace cF = new CFace();
Would these be true?
o1 = iF
o1 = cF
iF = cF
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused by "I would think that #2 would be correct and #2 and 4 would be false." You said that #2 is correct but #2 and #4 are not? I don't understand what you don't understand.
Firstly, all objects can be assigned to an Object reference. That validates 2 choices. Then an instance can be assigned to the interface reference type that the instance implements.
Hope this helps.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic