• 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

Object Casting

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

Class Z {}
Class X { Z z = (Z) new X(); }

is it possible to run operators like ==, instance of, meaning after doing Object Type casting, can that (i.e. z in this case)reference of the class being casted ,able to use the class( since Z know nothing about X rather then just a CAST). like here , can reference of Z use all the methods of X once it's converted to X(). please explain if anybody would like.

Thank you
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Typecasting X to Z without X extending Z would give you a Compile time Error (inconvertible types).
But in case X extends Z, then the instance z is allowed to use all methods of X. (like a normal extended class)

Class Z {}
Class X extends Z{ Z z = (Z) new X(); }

Does this answer your question?
 
chintan ramavat
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup that was it sridher, thank you very much
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sridher Reddy:
But in case X extends Z, then the instance z is allowed to use all methods of X.



That is not correct. Without an explicit cast, you can use only methods that are declared in Z.

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