Re: What about real polymorphism ?? [message #41968 is a reply to message #41965] |
Thu, 09 December 2004 12:25   |
Michael Wallace
Messages: 409 Registered: December 2003
|
Senior Member |
|
|
> I don't know a thing about JAVA, but didn't this just
> DO what you want it to do?
Quite a thread you guys have here. Anyway, I can't speak for objects in
IDL since I haven't actually learned how to use them yet, although I
keep planning to. I know exactly what Antonio is saying, so I'm going
to try and give another example of how things work in Java. Maybe
seeing this will help you IDL folks to better understand the Java side
of the question. I'm going to be using Java code in my example, so I
hope you can follow it. ;-)
I'll define three classes: Shape, Square and Circle. The Shape class
defines a method called area() which returns the area of the shape. As
a default, the area() method of Shape will return 0.0. Square and
Circle both inherit from Shape and override the area() method.
public class Shape {
public float area() {
return 0.0;
}
}
public class Square extends Shape {
private float length;
public float area() {
return length * length;
}
}
public class Circle extends Shape {
private float radius;
public float area() {
return Math.PI * radius * radius;
}
}
Now let's say that we had a utility class that works with Shapes and one
of its methods creates a nice string representation of the shape for us.
public class ShapeUtil {
public static String printShape(Shape s) {
return "Shape area = " + s.area();
}
}
This class is totally unaware of what kind of Shape was originally
instantiated. The Shape could just be an instance of Shape or it could
be an instance of Square or Circle. Even though we don't know the type
of Shape sent in, each Shape knows its own type and so it calls the
area() method specific to its type. If the Shape happens to be a
Circle, the area() method of Circle is called. If Shape happens to be a
Square, the area() method of Square is called. We don't need to know
which specific Shape was instantiated in order to get the area of the Shape.
I believe the original poster was trying to say that in IDL when you
have a case like this, where you only have knowledge of the superclass,
the superclass method was getting called instead of the overriding
subclass method even if the object was instantiated as the subclass.
Again, I don't know what happens in IDL since I haven't yet learned how
objects work in IDL, but I believe this describes the behavior the
original author was seeing. Hope all this makes sense. If not, feel
free to ignore this post -- that'll be nothing new for me. ;-)
-Mike
|
|
|