polymorphism object type checking

B

Bob Falooley

Hi,
It seems like I knew how do do this once, please point me in the
right direction.

I have an array of nodes, which are instances of interface objects for
differant kinds of nodes.

GenericNode
|
|---DataNode
|
|---MetaDataNode


I only want to use nodes of one particular type, so how do I check the
type of an object?

I want to do something that looks like this, but I don't know if a
command like this exists:

interfaceNode myNode = GetRandNode()

if (myNode.isOfType( DataNode ) )
//do something with node of interest
 
L

Lee Weiner

Hi,

I want to do something that looks like this, but I don't know if a
command like this exists:

interfaceNode myNode = GetRandNode()

if (myNode.isOfType( DataNode ) )
//do something with node of interest

You're talking about the instanceOf operator:

if( myNode instanceOf DataNode )

Lee Weiner
lee AT leeweiner DOT org
 
S

SMMT

Bob Falooley said:
Hi,
It seems like I knew how do do this once, please point me in the
right direction.

I have an array of nodes, which are instances of interface objects for
differant kinds of nodes.

GenericNode
|
|---DataNode
|
|---MetaDataNode


I only want to use nodes of one particular type, so how do I check the
type of an object?

I want to do something that looks like this, but I don't know if a
command like this exists:

interfaceNode myNode = GetRandNode()

if (myNode.isOfType( DataNode ) )
//do something with node of interest

Althought instanceof is a possibility but is not polymorphism.
For real use of polimorphism you need to create a method like

doSomethingWithNode ( DataNode node )
doSomethingWithNode ( MetaDataNode node)

For this to work DataNode and MetaDataNode need to share an ascendent. One
of them could be the parent class of the other of both extentions of a
common class (GenericNode).

When you pass the node the correct method will be invoked. This is real
polimorphysm.

The code will be something like:

GenericNode node = getRandNode();
NodeProcessor processor = new NodeProcessor();

processor.doSomethingWithNode( node);


(PS I didn't understand from here comes interfaceNode )
 
B

Bernhard Pfahringer

Althought instanceof is a possibility but is not polymorphism.
For real use of polimorphism you need to create a method like

doSomethingWithNode ( DataNode node )
doSomethingWithNode ( MetaDataNode node)

For this to work DataNode and MetaDataNode need to share an ascendent. One
of them could be the parent class of the other of both extentions of a
common class (GenericNode).

When you pass the node the correct method will be invoked. This is real
polimorphysm.

The code will be something like:

GenericNode node = getRandNode();
NodeProcessor processor = new NodeProcessor();

processor.doSomethingWithNode( node);

Be very careful when you do that kind of thing.
Overloading only gives you compile-time polymorphism.
If you want to use the actual runtime type of a parameter,
you can use instanceof for quick-and-dirty hacks, or
study the visitor pattern and use that as a guideline.
If you don't believe, run the following, and inspect its output:

class Generic {}
class Sub1 extends Generic {}
class Sub2 extends Generic {}

public class OverloadTest {

static private void f(Generic g) {
System.out.println("generic");
}
static private void f(Sub1 s) {
System.out.println("sub1");
}
static private void f(Sub2 s) {
System.out.println("sub2");
}

public static void main(String[] args) {

f( new Sub1());
f( new Sub2());

Generic g = new Sub1();
f(g);
f( (Sub1) g);

g = new Sub2();
f(g);
f( (Sub2) g);
}
}

cheers, Bernhard
A
A
A
A
A
A
A
A
A
A
A
A
A
A

B
B
B
B
B
A
D
study the visitor patterns and use that as a guideline.
 
R

Ryan Stewart

Bernhard Pfahringer said:
Be very careful when you do that kind of thing.
Overloading only gives you compile-time polymorphism.
Is that really considered polymorphism? I wouldn't call it that. SMMT, this
is polymorphism put simply:
class Node {}
class DataNode extends Node {}
class MetaDataNode extends DataNode {}

public class Tree {
public void doSomething(Node node) {
System.out.println(node);
}

public static void main(String[] args) {
Tree t = new Tree();
Node n = new Node();
t.doSomething(n);
n = new DataNode();
t.doSomething(n);
n = new MetaDataNode();
t.doSomething(n);
}
}

To extend the example, add a method to Node and override it in DataNode and
MetaDataNode and call it via "n" each time it is reassigned.
 
B

Bob Falooley

thanks for the replies.

While I was waiting for google to post my question, I was snooping
around and found I could call:

String name = node.getClass().getName();

then compare the name of the class and act on it accordingly.

I realize it is a hack, and not true polymorphism, but I don't have
access to the code for the nodes that I need to traverse, so I am just
hacking.

Since it works now I probably won't bother to use instanceof but I
apreciate you all reminding me of the keyword.

--Falooley
 
J

Joona I Palaste

Bob Falooley said:
thanks for the replies.
While I was waiting for google to post my question, I was snooping
around and found I could call:
String name = node.getClass().getName();
then compare the name of the class and act on it accordingly.
I realize it is a hack, and not true polymorphism, but I don't have
access to the code for the nodes that I need to traverse, so I am just
hacking.
Since it works now I probably won't bother to use instanceof but I
apreciate you all reminding me of the keyword.

Hey, whatever works, but I find instanceof much cleaner than using that
kind of hack. If you ever subclass your node class, your above hack will
break, but instanceof checking won't.
 
I

iamfractal

Hi,
It seems like I knew how do do this once, please point me in the
right direction.

I have an array of nodes, which are instances of interface objects for
differant kinds of nodes.

GenericNode
|
|---DataNode
|
|---MetaDataNode


I only want to use nodes of one particular type, so how do I check the
type of an object?

I want to do something that looks like this, but I don't know if a
command like this exists:

interfaceNode myNode = GetRandNode()

if (myNode.isOfType( DataNode ) )
//do something with node of interest


Notwithstanding the fine answers you've already received, the subject,
"Polymorphism object type checking," is a little self-contradictory,
as polymorphism is a tool designed to avoid the need to check object
types.

If you have to do it, your, "instanceof," is the easiest way, but - as
I'm sure you've seen from various examples - don't sprinkle, "if ...
instanceof ...," lines throughout your code, as then, if a new
descendent is introduced, some of these if-statements may break.

Also, by having something like: if (myNode instanceof DataNode) ...

then you're testing for a property of DataNode outside of the class
DataNode: encapsulation would suggest that this type of check is best
left to the Node descendents themselves, as they know exactly which
type of node they are.

Always cleaner (despite the overhead) is to have a doSomething()
method that does nothing except in those classes that you're
interested in. That way, any new descendents can handle themselves and
there need be no change to existing code.

class GenericNode {
public void doSomething() {
}
}
class DataNode extends GenericNode {
public void doSomething() {
System.out.println("Only interested in DataNodes");
}
}
class MetaDataNode extends DataNode {
public void doSomething() {
}
}

class Main() {
public static void main(String[] args) {
List list = new ArrayList();
list.add(new GenericNode());
list.add(new DataNode());
list.add(new MetaDataNode());

// Scan through and only print DataNode
for (int i = 0, n = list.size(); i < n; i++) {
GenericNode node = (GenericNode)list.get(i);
node.doSomething();
}
}
}


..ed

www.EdmundKirwan.com
 
B

Bob Falooley

(e-mail address removed) (Bob Falooley) wrote in message

Notwithstanding the fine answers you've already received, the subject,
"Polymorphism object type checking," is a little self-contradictory,
as polymorphism is a tool designed to avoid the need to check object
types.

If you have to do it, your, "instanceof," is the easiest way, but - as
I'm sure you've seen from various examples - don't sprinkle, "if ...
instanceof ...," lines throughout your code, as then, if a new
descendent is introduced, some of these if-statements may break.

Also, by having something like: if (myNode instanceof DataNode) ...

then you're testing for a property of DataNode outside of the class
DataNode: encapsulation would suggest that this type of check is best
left to the Node descendents themselves, as they know exactly which
type of node they are.

Always cleaner (despite the overhead) is to have a doSomething()
method that does nothing except in those classes that you're
interested in. That way, any new descendents can handle themselves and
there need be no change to existing code.

class GenericNode {
public void doSomething() {
}
}
class DataNode extends GenericNode {
public void doSomething() {
System.out.println("Only interested in DataNodes");
}
}
class MetaDataNode extends DataNode {
public void doSomething() {
}
}

class Main() {
public static void main(String[] args) {
List list = new ArrayList();
list.add(new GenericNode());
list.add(new DataNode());
list.add(new MetaDataNode());

// Scan through and only print DataNode
for (int i = 0, n = list.size(); i < n; i++) {
GenericNode node = (GenericNode)list.get(i);
node.doSomething();
}
}
}


.ed

www.EdmundKirwan.com

Thanks for the info, I can tell you guys love this OO stuff, and I
apologize for hacking it. But for my purpose right now, it is not
worth it for me to subclass all of the classes for nodes and add
methods.

But I have learned how to do it correctly from you guys, so if I am
ever implementing something like this from scratch, I will use your
advice.

--Falooley
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top