Casting Objects
by , 04-26-2012 at 05:36 PM (762 Views)
As it has been shown that object is class’s data type, from which instantiation took place. If we write, for example:
myBike is of type, MountainBike.Java Code:public MountainBike myBike = new MountainBike();
MountainBike get descended from Object & Bicycle. Hence, MountainBike is an object as well as a Bicycle. It is used when Object or Bicycle objects are called.
It is not necessary that reverse is true: Bicycle might be MountainBike, however not necessary. Same is the case with an object which may be a MountainBike or Bicycle, however not necessary.
An object of 1 type usage rather than the other type is shown by the casting, among those objects which are allowed by implementations & inheritance. E.g. if we write:
Obj is both MountainBike & Object. This is known as implicit casting.Java Code:Object obj = new MountainBike();
We write, at the other hand:
Compile time error will come as obj is unknown to compiler, to be MountainBike. But compiler which is assigned MountainBike to obj, by explicit casting:Java Code:MountainBike myBike = obj;
Java Code:MountainBike myBike = (MountainBike)obj;









Email Blog Entry
sorry for all the questions
thanks...
06-14-2013, 02:22 PM in gbonecapone