File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Creating References to Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating References to Objects" Watch "Creating References to Objects" New topic
Author

Creating References to Objects

Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 44
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?:

NewObject sampleNewObject = (NewObject) newObjectFactory.createNewObject();

(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?

Thanks,

Landon
[ October 15, 2004: Message edited by: Landon Blake ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Landon,

Welcome to JavaRanch!

As to your question #1, you can just write


NewObject sampleNewObject = newObjectFactory.createNewObject();

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

TestInterface ti = (TestInterface) collection.get(0);
ti.someMethodDefinedInTestInterface();

This gets the first item from this collection, and tells the compiler it's OK to treat the raw Object as a TestInterface.


[Jess in Action][AskingGoodQuestions]
Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 44
Thanks for the response Ernest.

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:

Monster greenGodzilla = (Godzilla) monsterFactory.createScaryMonster();

If instead I want a reference to a Cyclops Monster object name oneEye I would use:

Monster oneEye = (Cyclops) monsterFactory.createScaryMonster();

Finally, if all I wanted was a reference to a generic monster object named
anyOldMonster I could simply use:

Monster anyOldMonster = monsterFactory.createScaryMonster();

Are all the above scenarios correct?

Thanks again for the assistance,

Landon
[ October 15, 2004: Message edited by: Landon Blake ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Landon,

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.)

I hope this makes sense... :roll:
[ October 15, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Landon Blake
Ranch Hand

Joined: Oct 15, 2004
Posts: 44
Marc,

This make perfect sense. Thanks for the clarification, and for using my example. You guys do a great job!!

Landon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating References to Objects