• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

References and casting

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a bunch of definitions and reference declarations from jqplus6:



1. Let's say that we do the following:

o3 = o1;

It won't compile because superclass reference cannot be assigned to subclass reference without explicit cast. So I did the cast:

o3 = (C3)o1;

And Eclipse wrote:

Syntax error on token "o3", VariableDeclaratorId expected after this token.

Why?


2. There is another assignment:

o3 = o2;

It also seems to be not correct. Why? Both o2 and o3 IS A interface I2 so...?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if you wrote the code correctly, but the last 3 code is written outside any class/interface definition
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ismael Upright:
Here's a bunch of definitions and reference declarations from jqplus6:

1. Let's say that we do the following:

o3 = o1;

It won't compile because superclass reference cannot be assigned to subclass reference without explicit cast. So I did the cast:

o3 = (C3)o1;

And Eclipse wrote:

Syntax error on token "o3", VariableDeclaratorId expected after this token.

Why?


becuase you cannot downcast an object , you can only upcast
it means you can do
Parent obj = new ChildBoj()
or
Parent obj = (Parent) new ChildObj();
but not
Child obj = new Parentobj()
or Child obj = (ChildObj) new ParentObj()



2. There is another assignment:

o3 = o2;

It also seems to be not correct. Why? Both o2 and o3 IS A interface I2 so...?



in this case 03 is an object of type C3 and o2 of C2 and not of type interface I2
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I figured out my mistake, it was lack of the main method

After fix the code looks like this:



It is possible to make a downcast.

But still I don't understand why we cannot assign o3 to o2. For me it's assigning an object which IS-A interface I2 to the reference which also IS-A interface I2 so it should work.

Isn't it?
[ April 21, 2008: Message edited by: Ismael Upright ]
 
Arie Prastowo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my understanding, you can't do that because o3 was declare as C3 so it expected also C3 and all it's subclasses
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But C3 was also declared as I2, which means that every C3 IS-A I2..
 
Arie Prastowo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you said C3 IS-A I2, only valid in class definition where C3 IS-A C1 and I2.
But when you declare
static C3 o3;
the compiler expect something that come from C3 or it subclasses.
It would make a different if you code
static I2 o3;
since it can accept anything that implement I2 including o2
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right, I forgot about that.


Thanks!
 
it's a teeny, tiny, wafer thin ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic