• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Having problem in understanding casting involved in Inheritance

 
Snehal Rane
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello.

i am preparing for OCAJP 7 exam. I have completed studying from Mala Gupta's book and have started using Enthuware Test Studio offline for mock tests...attempting objective-wise tests...not yet started practice or standard tests.
Every time before attempting i read that particular chapter from Mala Gupta's book and my separate notebook where i note down new(unknown to me and not mentioned in book) concepts or rules i come across in explanation provided in enthuware(text in blue mostly). Whenever i encounter problem i code and clear my doubts.

i am fine with most of topics even including inheritance while reading MG's book, but while attempting mock test on inheritance i am facing lot of problems...i get very confused...

the topic of difference between TYPE OF REFERENCE and TYPE OF AN OBJECT and
that we can use a reference variable of a base class to refer to an object of its derived class is pretty clear as its explained very nicely by Mala Gupta
DC obj= new DC(); // DC =Derived Class
BC obj= new DC(); //BC= Base Class
InterfaceName I = new DC();//DC class is implementing interface InterfaceName here

But the confusion starts when Casting is involved with above scenario...
i am not able to understand when its compile error and when runtime error and why?

few things i always recollect are that -
we cannot cast object of BC to object Dc, even if we did it will compile but not run as it never points to that particular class.
and having various combinations of DC is on l.h.s. and B.C. on r.h.s. or both , etc and getting their objects involved in assignments are increasing my confusion.

i am feeling as if i do not know anything related to casting.

i need someone to help me by explaining in detail about casting and its related rules so as to understand
when to cast
when not to cast
how to cast
when its valid/invalid
all at one place
which will help me attempt the test better.


 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Snehal Rane,

First of all, a warm welcome to the CodeRanch!

This thread and this one have great explanation and examples about casting. So I would first read them thoroughly and it will answer some (maybe all) of your questions.

If you still have doubts/questions after reading both topics, don't be afraid and just ask them here. We are all here to help you!

Hope it helps!
Kind regards,
Roel
 
Snehal Rane
Greenhorn
Posts: 6
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much for such an instant reply.
I will definitely go through the threads you have suggested and if I have doubt where else I am gonna get such a great help... I will post them here ofcourse....
Thankyou once again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic