View RSS Feed

Core Java

Object Reference Type Casting

Rate this Entry
by , 03-02-2012 at 04:27 PM (3142 Views)
In Java object typecasting, type cast of 1 object reference is possible to another object reference. Suck type of casting is done with superclass, subclass or class types. Compile time rules or runtime rules are present in Java for casting.

Class relationship is the basic thing at which Object reference is dependent. Any object reference could be assigned to reference variable of type object because the object class is usually the superclass, for each Java class.

Two possible casting java scenarios have been given as under:

1. Up-casting
2. Down-casting

When casting of referencing is done in some direction with class hierarchy, it is considered as downcast i.e. from root class to subclass. When casting of reference is done in some direction with class hierarchy i.e. from subclass to root, it is considered as upcase. Cast operator is not needed in such kind of case.
Compile time rules catch up attempted casts, in those particular situations where it is not possible. It happens specially attempts are made to cast at objects which are totally un-related. When casted objects are not found compatible with newly casted ones, ClassCastException is thrown.

Submit "Object Reference Type Casting" to Facebook Submit "Object Reference Type Casting" to Digg Submit "Object Reference Type Casting" to del.icio.us Submit "Object Reference Type Casting" to StumbleUpon Submit "Object Reference Type Casting" to Google

Tags: type casting Add / Edit Tags
Categories
Casting

Comments