• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating References to Objects

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Landon Blake
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Landon Blake
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marc,

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

Landon
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic