Showing posts with label implicit cast. Show all posts
Showing posts with label implicit cast. Show all posts

Sunday, September 16, 2012

Casting


DOWNLOAD CODE PROJECT: Casting

Casting can be used to convert an object of one class type to another, within an inheritance hierarchy.

For instance, all Java objects inherit from the Object class. A class called Player may be implicitly cast into the type Object, as below:

Object o = New Player();

However, the following statement will not compile:

Player P = o;

This is because a Player object is always an instance of Object whereas an Object is not always an instance of Player (An Object is alllll kinds of things).

We need to tell the compiler that o is an instance of Player, as below:

Player P = (Player) o;

This is called explicit casting - or down casting. Note that the type we are casting to is included in the parentheses.

The Animal family
Consider a family of classes where Animal is the superclass and it has sub classes of Mammal and Fish. In this scenario, Animal is the generalisation and Mammal & Fish are the specialisation.



We could, for instance, create an ArrayList to store all types of Animal, as below:

ArrayList<Animal> animalList = new ArrayList<Animal>();

and to that list we could add any type of animal:

animalList.add(New Mammal());
animalList.add(New Fish());

When the Mammal & Fish object types are added to the ArrayList, they have been implicitly cast as Animal. This means that they may only be used in the context of Animal - only state and behaviour belonging to Animal will be accessible from theses objects (even though we can see their actual type, on the screen in front of us).

If we wish to use these objects as their specialised type, we must first discover their run time instance type and then use explicit casting - down casting, to convert.

Consider the following inheritance hierarchy:



(NOTE: The diagram shows the hierarchy incorrectly - Mammal and Fish should be swapped :P)

There are now 7 types of Animal and we have instantiated each type into the ArrayList, as below:

ArrayList<Animal> animalList = new ArrayList<Animal>();
animalList.add(New Animal());
animalList.add(New Mammal());
animalList.add(New Fish());
animalList.add(New Bass());
animalList.add(New Snapper());
animalList.add(New Wombat());
animalList.add(New Kangaroo());


Let's say we want to search the list for certain types of Animal - remember, currently the compiler sees them all as the generalised type - Animal, because that is the type of the ArrayList. 

We could look for specific types, such as Bass. Or we could look for types of specialised type - for instance, finding all types of Fish.

Getting the specific type - equals
In the example below, a loop is used to traverse the list of Animal, looking for objects of the specialised type Bass. 

The equals() comparison method is used on each object to evaluate whether it's run time class is equal to Bass.


Note the statement:


The getClass() method returns the run time class of the object. The equals() method then compares that type to the type mentioned in the parentheses. In this case, Bass.

In the next statement, if the evaluation is true (if the currentAnimal object was found to be of type Bass), the Animal object (currentAnimal) is explicitly cast to an object of type Bass (bass).



The bass object now has the context of Bass, meaning that it may access all state & behaviour of Bass, as well as Fish, as well as Animal.

Getting types of specialised type: instanceof operator
In the example below, a loop is used to traverse the list of Animal, looking for any objects which are types of Fish. Remember, Fish, as well as being a subclass of Animal, is also a superclass of Bass and Snapper.

The instanceof operator is used on each object to evaluate whether it's run time instance type is equal to Fish.



Note the statement:


The instanceof operator determines if the currentAnimal object is an instance of Fish, regardless of whether it's specific type if Bass or Snapper. This means that all objects of type Fish, Bass and Snapper will be discovered using this method.

In the next statement, if the evaluation is true (if the currentAnimal object was found to be an instance of Fish), the Animal object (currentAnimal) is explicitly cast to an object of type Fish (f).



The f object now has the context of Fish, meaning that it may access all state & behaviour of Fish, as well as Animal.