Srb1313711 Srb1313711 - 5 months ago 8
Java Question

Java why instantiate a class using object?

I am currently revising for a Java se7 certification and anyone who has done any of these exams knows how ridiculous some of the code samples they use are. Purposely writing code that is awkward to understand.
Recently I have seen alot of code written like this:

Object obj=new Animal();


What benefit does this provide? I understand this is valid code, however is this just one of those things that you would just never use like alot of the code in the exams? If not when and why would you do this?

rsp rsp
Answer

This is not something that is not useful. It shows that the Java type system lets you store any object in a variable of type Object and not only objects instantiated directly with new Object() and this is the simplest example that can demonstrate it.

Every object is a subclass of the Object class. It demonstrates that when you declare a type of a variable to be of some type, then you can put there not only that exact type (Object in your example) but any subtype (Animal in your example).

While using the base class for all objects may not seem useful, a more complicated class hierarchy may demonstrate it better.

Let's say that you have more subclasses of Animal: Dog, Cat, Hamster. Now let's say that you have also many subclasses of Dog, Cat and Hamster (if there are any different kinds of hamsters).

public class Animal { // implicitly extends Object
    // ...
}
public class Dog extends Animal {
    // ...
}
public class Cat extends Animal {
    // ...
}
public class Hamster extends Animal {
    // ...
}
public class Hamster extends Animal {
    // ...
}
Labrador extends Dog {
    // ...
}
// etc.

Now, if you want to have an Animal in a variable you can write:

Animal animal = new Cat();

or:

Animal animal = new Labrador();

Now, the exact code from your question:

Object obj = new Animal();

can be useful in few different ways:

If you want to be sure that you are using the interface exposed by the Object class and nothing added later in the inheritance chain.

You can also have e.g. an array of 5 dogs:

Dog[] dogs = new Dogs[5];

where you can put dogs, but not cats or hamsters.

You can have an array of animals where you can put any animals, but not other Objects like e.g. numbers:

Animal[] animals = new Animals[5];

Now, why would you have such an array? For example if your Animal class has a getName method then you can always run:

animals[2].getName();

no matter which kind of animal is there. But if the Dog class has a method bark() but the Cat class doesn't have it, then you won't be able to run:

animals[2].bark();

(not without explicit casting) even if a Dog instance was indeed stored in animals under that index, because the compiler doesn't know what will be in that array during the run time - all it can guarantee is that there will be some animal there so you can only call those methods that are sure to be present for all animals.

This may sound silly but you'll see that it is indeed used in a lot of places. For example in GUI programming you can have a scroll panel in which you can put any component like a button or text field, but not any object at all, which is guaranteed by exactly that mechanism - for example a JPanel object has a method add() that take components of class Component but those doesn't have to be created with new Component() but can be e.g. a JLabel or another JPanel.

Comments