Re: Java/C++ question

From: Chris Smith (cdsmith_at_twu.net)
Date: 01/23/05


Date: Sat, 22 Jan 2005 23:48:23 -0700


 <dlukin@gmail.com> wrote:
> I'm not so much concerned with the commonality, but the differences -
> the extensions.
>
> Given:
> class A { public fooBar();}
> class B extends A {public fooBar(); public fooBar2();}
> class C extends A {public foobBar(); public fooBar3();}
>
> I have an array holding an instance of A, B, C. As I iterate across the
> array I want/need to be able to call different functions that each
> object implements.

Since the array contains three different kinds of object, how could you
possibly know which methods are there to call in the first place? Since
you don't know that fooBar2 exists, you can't possibly call fooBar2.
You can do this easily in neither C++ nor Java.

Now, if you know the specific set of subclasses, you can (in C++ or
Java) test to see which the object is, and cast appropriately. In Java:

    for (int i = 0; i < array.length; i++)
    {
        if (array[i] instanceof C) ((C) array[i]).fooBar3();
        else if (array[i] instanceof B) ((B) array[i]).fooBar2();
        else array[i].fooBar();
    }

The same can be done with RTTI in C++. That's generally a symptom of
poor design, though, in either language. The first question to ask is
why you didn't use polynorphism to achieve the same thing.

Java, in particular, can go even further with something called
reflection. Given the context, though, you almost certainly don't want
to do that.

> Implementing a common
> interface seems to give me access to the "commonality", but not the
> extensions...

If you explain how you expect to write code to use the "extensions",
then perhaps we could help. The toy code posted so far dodges the
issue. If you have NOT just created the object three lines earlier, and
therefore you don't know what the actual class of the object is, then
how what would the code look like?

The if statements in my example above are one option, and they work
fine. Is that what you're looking for, or something else?

-- 
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation


Relevant Pages

  • Re: Java Generic programming using subclassing
    ... Java nowhere guarantees that the elements of an array ... Now chances are that any Java ... > reference will be visible through the other. ... > eligible for garbage collection, and will in fact be collected before ...
    (comp.lang.java.programmer)
  • Re: java based supercomputer
    ... checking the correlation beteween an array of data and another array ... java psuedo remote threads will take a considerrably less time. ... Does your algorithm lend itself well to paralellization? ... the only bottleneck i can see is checking the correlation value ...
    (comp.lang.java.programmer)
  • Re: How to store a large amount of 3D data points in Java?
    ... >>None of which would be a problem if you could store ints in a Java ... > With 1.5 autoboxing you can, even if it is implemented with objects. ... ArrayList does array copy for resizing. ...
    (comp.lang.java.programmer)
  • Re: MinMax aus Integer-Array schnell ermitteln
    ... Trick "Loop unrolling" erreicht. ... Java sollte eigentlich an Delphi rankommen, ... Array UP ... private static void fillArray0() ...
    (de.comp.lang.java)
  • Re: MinMax aus Integer-Array schnell ermitteln
    ... Trick "Loop unrolling" erreicht. ... Java sollte eigentlich an Delphi rankommen, ... Array UP ... private static void fillArray0() ...
    (de.comp.lang.java)