Showing posts with label getClass(). Show all posts
Showing posts with label getClass(). Show all posts

Tuesday, January 8, 2013

Sorting types: Run time class

DOWNLOAD THE CODE: RuntimeTypeSearch

Consider the inheritance hierarchy in the diagram below:


A collection, such as ArrayList may be of a general type..

For instance:

ArrayList<Superclass> list = new ArrayList<Superclass>();

All objects of Superclass type may be added to the list.

For instance:

add(new Superclass());
add(new SubClassWhite());
add(new SubClassBlack());
add(new SubClassA());
add(new SubClassB());
add(new SubClassX());
add(new SubClassY());

The list now contains 7 objects, all of type Superclass. Even though many of the objects are specialisations of Superclass, they all have the context of Superclass.. This means we can only use each object as the general type (Superclass), regardless of its specialisation.

Getting the specific type of an object
If we want to discover the actual run time type of each object in the list, we call the getClass() method. This method is inherited by all objects from the Object class and returns the run time class of the object.

The equals() method (also inherited from Object) performs an equality check on the current object in the list, with the class name we are looking for.

if (list.get(6).getClass().equals(SubClassWhite.class) {
   //
}

If this expression evaluates to true, we are then able to cast the general object type into it’s specialised type:

SubClassWhite s = (SubClassWhite)list.get(6);

The object called s is now of type SubClassWhite and may be used in the context.

Full code listing (including a loop to iterate through the list) which finds objects in the list with the run time type of SubClassWhite and casts it accordingly.:

ArrayList<Superclass> list = new ArrayList<Superclass>();
add(new Superclass());
add(new SubClassWhite());
add(new SubClassBlack());
add(new SubClassA());
add(new SubClassB());
add(new SubClassX());
add(new SubClassY());

for (int x = 0; x < list.size(); x ++) {
   if (list.get(x).getClass().equals(SubClassWhite.class) {
       SubClassWhite s = (SubClassWhite)list.get(x);
   }
}

Getting the general type of an object
If we want to find all instances of SubClassWhite (remember that SubClassWhite is a subclass of Superclass but also a superclass itself) we use the instanceof operator. Using this method, all instances of SubClassWhite, SubClassX and SubClassY will be discovered.


for (int x = 0; x < list.size(); x ++) {
   if (list.get(x) instanceof SubClassWhite.class) {
       SubClassWhite s = (SubClassWhite)list.get(x);
   }
}

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.