Hi,
I still don't get it. Please help.
So, the code is:
Code: Select all
class CorbaComponent{
String ior;
CorbaComponent(){ startUp("IOR"); }
void startUp(String s){ ior = s; }
void print(){ System.out.println(ior); }
}
class OrderManager extends CorbaComponent{
OrderManager(){ }
void startUp(String s){ ior = getIORFromURL(s); }
String getIORFromURL(String s){ return "URL://"+s; }
}
public class Application{
public static void main(String args[]){ start(new OrderManager()); }
static void start(CorbaComponent cc){ cc.print(); }
}
I understand that in method in the main-method, the method start() is called and a new reference-variable cc is created in method start():
Corbacomponent cc = new OrderManager();
I also understand that in overridden methods, such as cc.print(), the compiler first checks if the class from the reference-variable has this method [answer: yes, class CorbaComponent has a method print()]. And second, that the actual method that gets executed in runtime is on the basis of the method of the object [and yes, class OrderManager also has a method print() because it inherits it from the class CorbaComponent].
However, before we get get to overridden method cc.print(), first some other things happen:
So, starting at the beginning, you start in the main-method where 'new OrderManager()' is a call to the construct of class Ordermanager. This, in turn, calls its superclass-constructor CorbaComponent(). There, method startUp() is called and String "IOR" is passed as argument. So far, it is clear.
Here is where I get confused. We are now in class CorbaComponent so I would think that therefore "IOR" gets passed to 'String s' in method startUp in class CorbaComponent. I don't see why OrderManager's startUp() is called.
Your words are: "The method selection is done on the basis of the actual class of the object (which is OrderManager here)." when deciding which method is called with "IOR" as parameter - CorbaComponent's startUp or OrderManager's startUp.
But in my view, what happens with String "IOR" in method startUp() has nothing to do with object cc that is created in method start() [which is even a different method from startUp] and also has nothing to do with the polymorphism that happens with cc.print(). So basically I don't see why String s = "IOR" is treated polymorphically.
Can you please explain why I am wrong, or what I am missing?