• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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.

 
it's a teeny, tiny, wafer thin ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic