Creating References to Objects

Landon Blake

Ranch Hand

Posts: 44

posted 13 years ago

I had a couple quick questions about how to create references to objects.

1. In the first scenario, I want to create a reference to an object of the type NewObject. I want to set this reference equal to the return value of the createNewObject method of the newObjectFactory object. Would my code look like this?:

(P.S. - The createNewObject method is defined to return a NewObject object.)

2. I have a collection of implementInteface objects that implement an interface called TestInterface. When I return an object from this collection, how do I call a method of the TestInterface, since the collection returns a generic object? I think there is some type of casting involved, but I'm not sure what the format of the code would look like. Can anyone help me with this?

I took out the "(NewObject)" which isn't needed here, as you say that createNewObject() already is declared to return type NewObject. That construct is called a "cast", and what it does is tell the compiler to treat a reference of one type as if it were a reference to some other type. Here, the types match.

As to #2, I can't say I understand the details exactly, but you may simply need to do something like

You answered both of my questions. I was confused about the appropriate use of that argument in parentheses on the right side of the equal sign. (What is the proper name for it? - a "construct"?) But I think I've got it now. You only need to use that argument when you are casting to a type other than the one called by the "constructor" method. Is this correct?

For Example:

I have a class named Monster, and an object named monsterFactory that implemements a createScaryMonster() method that is used to return Monster Objects. I also have 2 subclasses or subtypes of Monster, (1)Cyclops and (2)Godzilla.

If I want to create a reference to a Godzilla Monster object named greenGodzilla I would use:

The parenthetic preceding the object on the right of the equals sign is called an "explicit cast," because you're explicitly "casting" the type of the object. Let me try to explain when this is needed...

The createScaryMonster() method is returning an object of type Monster. If you're assigning this to a variable declared as type Monster, then no cast is needed because you're handing the variable exactly what it expects. (Or more accurately, what the compiler expects for that variable.)

Monster someMonster = monsterFactory.createScaryMonster();

In fact, if you're assigning the Monster object to a variable declared as a parent class of Monster, then you still don't need an explicit cast; because upcasting (from subclass to parent) is "safe" and can be achieved automatically through "assignment conversion."

For example, suppose you had a parent class called Creature, with Monster extending Creature. Now, if your variable is expecting a Creature, and you hand it a Monster, it's still okay without an explicit cast because every Monster is a Creature.

Creature bigCreature = monsterFactory.createScaryMonster();

Note that although there is no explicit cast here, the Monster being assigned to the variable is being implicitly upcast (automatically converted) to the parent type Creature.

So when do you need an explicit cast? When you're going the other way -- when you're downcasting. If your variable is expecting a Godzilla, and you hand it a Monster, then there might be a problem because not all Monsters are Godzillas. So here you need to assure the compiler that the Monster you're assigning is, in fact, an instance of Godzilla.

Godzilla bigG = (Godzilla)monsterFactory.createScaryMonster();

Now, the explicit cast to type Godzilla will satisfy the compiler. (However, if you're wrong, and the Monster you're passing actually turns out to be a Cyclops, then you'll get a RuntimeException.)