View RSS Feed

Java Object

Casting Objects

Rate this Entry
by , 04-26-2012 at 06:36 PM (1245 Views)
As it has been shown that object is classís data type, from which instantiation took place. If we write, for example:
Java Code:
public MountainBike myBike = new MountainBike();
myBike is of type, 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:
Java Code:
Object obj = new MountainBike();
Obj is both MountainBike & Object. This is known as implicit casting.

We write, at the other hand:
Java Code:
MountainBike myBike = obj;
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 = (MountainBike)obj;

Submit "Casting Objects" to Facebook Submit "Casting Objects" to Digg Submit "Casting Objects" to Submit "Casting Objects" to StumbleUpon Submit "Casting Objects" to Google