• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Ques on casting referances ...

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Please explain results of the follwing 2 ques ...

The programs throws a java.lang.ClassCastException at the line labeled (3) when run.
Why the exception comes even if an explicit cast (B[]) is made at line labeled (3) ??
Here is another program on polymorphism :

The program will print 2 when run.
Please explain why so ?? why not 1 when explicit cast of (B) has been provided ??
Thanks in advance,
Indraneel
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to Javaranch, a friendly place for Java greenhorns
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Indraneel Das
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem, I have changed my displayed name.
thanks
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Daas
in first program if you note the diffrence in line (2) and line (3) is that in line (2) arrA is refrencing to array object of type B.
So line (2) is correct.
But for line (3) your arrA is refrencing to array object of type A and casting of base class object to subclass refrence var is not possible. So it will throw ClassCastException.
Second program is example of polymorphism. Method will be called as per the type of object not the reference var type.
If you are not clear then you can ask again
I will suggest you to go through RHE book atleast once.
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, your questions are simple and basic, you need to read more about reference casting. I'll try to give you a brief, but I strongly recommend that you read in details and more in depth about casting.
First, you have to know the difference between Reference type and object type:
if C extends B, B extends A, then:
B refB = new C();
refB has a reference type B and object runtime type C.
1- You can NOT cast an object of object runtime type B to a refernce type C even with explicit casting. Upcasting (object runtime wise) is not allowed. It will compile fine but will throw a runtime exception "ClassCastException", that�s because at compile time the compiler doesn�t know what is the actual runtime class of the object.
2- In case of B refB = new C(); the reference refB runtime type is C, so C refC = (C) refB; is legal, since you assign the reference of type C to an object of actual runtime type C as well.
3- In all cases, A refA = refB; & A refA = refC; is always accepted, downcasting is always permited without even explicit casting.
4- A refA = new C(); B refB = (B) refA; is also accepted it's downcasting from type reference A to type reference B, since the actual runtime type of refA is actually type C and C is a subclass of B.
5- 1.There is a difference between members overriding and members hiding. Member Variables and Static Methods are hidden, while Instance Methods are overridden. If an object of a subclass is instantiated with a super class reference the difference appears between hiding and overriding.
On calling members using the �Super� Object Reference of a �Sub� Object the value of the Member Variables and the implementations of the Static Methods in the Super Class are used, while the implementations of the Instance Methods in the Subclass are used.
6- 2.If a method that is not overridden or hidden is called on an object of a subclass � no matter what the reference type is �, the implementation of this method is in the body of the super class of course. If this method calls a method that is hidden (static) in the subclass, the implementation of the hidden method in the super class is used. But if the method � the first one � calls a method that is overridden (instance) in the subclass, the implementation of the overridden method in the subclass is used, having in mind that the object is of the subclass and no matter what reference type it is.
7- 3.If an object of a subclass is instantiated with a reference of a super class and a method that is defined in the subclass but not the super class is invoked on the object, a Compile-time error appears (Can�t resolve symbol), that�s because at compile time the compiler doesn�t know what is the actual runtime class of the object and so has to make sure the method exists in the definition of the object reference type class.
read more on inheritance and casting.
HTH
[ October 19, 2002: Message edited by: Alfred Kemety ]
 
Indraneel Das
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alfred and Ravish for the information ... they are really very helpful to me and cleared my doubt ... but i have to study more to have confidence ...
Thanks to all
reply
    Bookmark Topic Watch Topic
  • New Topic