About Question enthuware.ocajp.i.v7.2.1198 :

Moderator: admin

Post Reply
UmairAhmed
Posts: 9
Joined: Mon Apr 28, 2014 9:16 am
Contact:

About Question enthuware.ocajp.i.v7.2.1198 :

Post by UmairAhmed » Mon May 26, 2014 12:15 pm

Hi,

Need your urgent help if you can :idea:
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(); }
}
The instance variable has been initialized with the value URL:// IOR ... but after that I couldn't get this
static void start(CorbaComponent cc){ cc.print(); }
method start() has been called with and reference of OrderManager object has been passed to it as an argument. Right ? So
start(CorbaComponent cc)
, i mean how CorbaManager reference is passed , what am i missing here.

Can you please explain how it went further processing after the method Start() is called from main method.

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1198 :

Post by admin » Mon May 26, 2014 7:54 pm

Code: Select all

public static void main(String args[]){ 
 start(new OrderManager()); <--------- OrderManager IS-A CorbaComponent because it extends CorbaComponent therefore this actually invokes the static start(CorbaComponent cc) below

}

static void start(CorbaComponent cc){  <------- This method is invoked from main
 cc.print(); 
}
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1198 :

Post by heleneshaikh » Tue Oct 06, 2015 2:30 am

Hello,

Am I right about the order of execution? For some reason, my compiler doesn't go through every step. Thank you very much in advance.

class CorbaComponent{
String ior;
CorbaComponent(){ startUp("IOR"); } //3
void startUp(String s){ ior = s; }
void print(){ System.out.println(ior); } //8
}

class OrderManager extends CorbaComponent{
OrderManager(){ } //2
void startUp(String s){ ior = getIORFromURL(s); } //4 (overridden method)
String getIORFromURL(String s){ return "URL://"+s; } //5
}

public class Ape {
public Ape() { //1
}

public static void main(String[] args) {
start(new OrderManager()); } //6

static void start(CorbaComponent cc){ cc.print(); } //7
}

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1198 :

Post by admin » Tue Oct 06, 2015 9:08 am

You will need to add extra print statements and run the program to see which line is being printed first.
If you like our products and services, please help us by posting your review here.

heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1198 :

Post by heleneshaikh » Tue Oct 06, 2015 9:49 am

Thanks for the tip, everything's clear!

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1198 :

Post by Kevin30 » Sun Oct 01, 2017 10:32 am

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?

admin
Site Admin
Posts: 8621
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1198 :

Post by admin » Sun Oct 01, 2017 12:06 pm

You need to understand the execution of the main method step by step -
public static void main(String args[]){
start(new OrderManager());
}
Before the static start method of Application class can be invoked, the JVM has to create an OrderManager object because of the call to "new OrderManager()" as an argument to the start method.

1. When you do new OrderManager();, the constructor of OrderManager invokes super class's constructor. ( If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. )
2. The super class's constructor i.e. CorbaComponent's constructor contains a call to method startUp("IOR");
3. Since the object's actual class is OrderManager, the JVM invokes startUp method of OrderManager instead of CorbaComponent.
4. OrderManager's startUp calls ior = getIORFromURL(s);

Now, once the OrderManager is created, the JVM invokes Application's start method and passes the reference of the OrderManager object created above. The start method then invokes print on this OrderManager object, which prints the value of ior .
If you like our products and services, please help us by posting your review here.

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1198 :

Post by Kevin30 » Sun Oct 01, 2017 12:47 pm

Thank you for your explanation! I understand it now.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], noeloo and 0 guests