[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

determining the type of an object

Posted on 1998-11-05
1
Medium Priority
?
228 Views
Last Modified: 2010-03-30
Ok, here's my problem...
i've built a tree and now i'm walking down the tree and trying to extract values from the objects as i walk down it.
But my problem is that the method i call depends on the type of the object.  I would have to call intValue() if the object was of type Constant in my program b/c this type would contain an object Integer and i would charValue() if the type was Variable in my program b/c this would contain a Character.  Both Constant and Variable have a parent class called ETree.  How do I deterine what type of ETree i am looking at as I walk down the tree???
thanks!
0
Comment
Question by:wazila
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 16

Accepted Solution

by:
imladris earned 200 total points
ID: 1227012
You can use the instanceof operator. This is a Java keyword that takes an object and a class name, and results in true or false. So you could use it something like:

ETree object;

if(object instanceof Constant)i=intValue();
else if(object instanceof Variable)i=(int)charValue();



Alternatively, you could define an interface that each class implements so that you can call the same method, regardless of class type, that returns something appropriate. I don't know whether that is possible in your program, but it would be OOPier. Too much instancof'ing is frowned upon.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question