• 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:

How can This cast fail?

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Y can be cast to an A as (A)y, and at runtime it would be Y that was actually being assigned to Y. so shldnt the above work? How can I understand this? Ive been having alot of problems with this lately?
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are in misconception that narrowing of a primitive data type is same as upcasting of subclass to superclass you are wrong.

you can't cast a subclass class reference to a superclass always. See the following code:

A b=new B();
A y=(A)b;

only in above case you can do casting to a superclass object because actually b is a reference of class A, however it is refering to B class object.
 
Vaibhav Chauhan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you write:

y=(A)y;

you are assigning A class object to its subclass reference. The rule says that you can assign subclass object to its superclass reference but not the other way around.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
y is a B. You can assign a B to y or you can assign a subclass of B to y.
You cannot assign a super class of B to y.

From the Java Language Specification:


4.12.2 Variables of Reference Type
A variable of a class type T can hold a null reference or a reference to an instance of class T or of any class that is a subclass of T

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Barry



y is a B. You can assign a B to y or you can assign a subclass of B to y.
You cannot assign a super class of B to y.



Hope Y is a B was wrong in terms of your explanation. I hope it was B is a Y .

For example :

MyFrame is a Frame.

so


So Frame reference variable can hold Frame , MyFrame , is a MyFrame (subclass) or null
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The above declares y to be of type B. So y is a B. The actual object assigned to y is a B.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first of all y=(A)y will give you a compile time error,
you are converting a subclass ref to superclass type and storing the reference
in a variable y which is of type subclass.here the compiler will find
a type mismatch and complain of incompatible types.
but if you changeit to
x=(A)y
it will compile as now you are assinging it to a superclass reference
variable.
this will also work at runtime.ucan check it by applying the instaceof operator
as y instanceof x which holds true and thus it is a valid cast.
 
Arul Prasad
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Barry

thanks , now i got it , so your telling about the object of the particular type. but i wrongly thought it as a Inheritance Relations "is a" type of relation.



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic