Page 1 of 1

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

Posted: Sun Jun 03, 2012 5:52 pm
by patrickK
I understand the explanation of this question. However, I don't see why there are four lines of output, when println() is only called twice. How does getH() execute its println() when it's only used for its return value? Does using b.getH() and bb.getH() as parameters cause them to execute their println() calls? And if that is the case, wouldn't 4 get printed first, because it comes first in the first println() call in main()?

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

Posted: Sun Jun 03, 2012 8:00 pm
by admin
There is no parameter being passed to getH() calls. The code will not compile if you pass a parameter because the method declares no parameters.

How do you think a method will return a value? It returns a value after it executes. So in this case, both the getH methods (the base class one and the subclass one) have a system.out.println with in their body, which prints the additional two lines that you see.
For example, the line System.out.println(b.h+" "+b.getH()); actually prints two lines of output and not just one because one line of output is coming from the getH() method.

HTH,
Paul.

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

Posted: Fri Sep 14, 2012 1:42 pm
by Guest
I understand that instance variables and static methods are shadowed and that instance methods are overridden but I can not comprehend why the System.out.println() of Baap is printed before the first called b.h and again in bb.h.

In other words it seems like a number would be printed first, then the S.O.P. statement, then another number with the return.

Instead the S.O.P. prints then the individual numbers... 0_o

It almost seems like there is a static initialization block or S.O.P. statements in a constructor running but this is not the case.

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

Posted: Sun Sep 16, 2012 6:15 am
by admin
Guest wrote:I understand that instance variables and static methods are shadowed and that instance methods are overridden but I can not comprehend why the System.out.println() of Baap is printed before the first called b.h and again in bb.h.

In other words it seems like a number would be printed first, then the S.O.P. statement, then another number with the return.

Instead the S.O.P. prints then the individual numbers... 0_o

It almost seems like there is a static initialization block or S.O.P. statements in a constructor running but this is not the case.
This is what happens when the first SOP in main ( System.out.println(b.h+" "+b.getH()); )executes:
1. For the call to go inside the println method, you first need to compute the parameter that is to be passed. So, b.h+" "+b.getH() has to be computed first.
2. Now, to compute this string, you start putting the values of the 3 components -
4 + " "+b.getH() <-- So you see that b.4 is indeed accessed first as you say.
3. Now, you need to execute b.getH() to complete the third component. It returns 44 but this method contains a println of its own, so before returning 44, it prints "beta 44".
4. So now, your string becomes - 4+ " "+44, which is then printed out.

HTH,
Paul.

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

Posted: Sun Sep 16, 2012 12:10 pm
by Javanaut
Hi Paul,

Thank-you sir for the reply. I wrote a similar class and obtained the same behavior. Thank-you for the explanation, that made sense. I was not expecting the method's SOP to print before the SOP in main().

:shock: :mrgreen:


Respectfully,

Javanaut

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

Posted: Mon Jan 21, 2013 11:43 pm
by Guest
Could you please explain why following code not function in same manner?

Code: Select all

class Car{
   public int gearRatio = 8;
   public String accelerate() {  return "Accelerate : Car";  }
}
class SportsCar extends Car{
   public int gearRatio = 9;
   public String accelerate() {  return  "Accelerate : SportsCar";  }
   public static void main(String[] args){
      Car c = new SportsCar();
      System.out.println( c.gearRatio+"  "+c.accelerate() );
   }
} 
Here output is 8 Accelerate : SportsCar

It prints the gearRatio value from the parent class

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

Posted: Tue Jan 22, 2013 7:04 am
by admin
Guest wrote:Could you please explain why following code not function in same manner?

Code: Select all

class Car{
   public int gearRatio = 8;
   public String accelerate() {  return "Accelerate : Car";  }
}
class SportsCar extends Car{
   public int gearRatio = 9;
   public String accelerate() {  return  "Accelerate : SportsCar";  }
   public static void main(String[] args){
      Car c = new SportsCar();
      System.out.println( c.gearRatio+"  "+c.accelerate() );
   }
} 
Here output is 8 Accelerate : SportsCar

It prints the gearRatio value from the parent class
Because unlike methods, variables are shadowed instead of overridden. So here, you are printing c.gearRatio, where c is of type Car. That is why Car's gear ratio is printed. By using a variable of super class, you are un-shadowing the Car's shadowed gearratio field.

HTH,
Paul.

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

Posted: Fri Feb 15, 2013 9:06 pm
by satar
Interesting, this could really be bad because if you have an overriding method that accesses an overshadowed variable, you can create a conundrum where you can update, in our example b.h but it never actually gets updated if you access it only through b.getH(). A very good example of one reason why you always want to encapsulate (make private) class variables. I would have never thought this kind of thing was possible. These exams are very clever about identifying idiosyncrasies with the language providing a major education in themselves!

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

Posted: Wed Feb 27, 2013 9:13 pm
by AlsoConfused.java
So the printout should be
for b.h (4 Beta 44 44) &
for bb.h (44 Beta 44 44)?

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

Posted: Thu Feb 28, 2013 7:24 am
by admin
AlsoConfused.java wrote:So the printout should be
for b.h (4 Beta 44 44) &
for bb.h (44 Beta 44 44)?
Yes.

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

Posted: Thu Mar 07, 2013 4:56 am
by The_Nick
Hi,
I would like to know why it prints out Beta 44 4 first and not 4 Beta 44 as I expected to be. Is it not executed from left to right? Thanks in advance


The_Nick

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

Posted: Thu Mar 07, 2013 5:00 am
by The_Nick
It must mean that the method getH() gets executed straight away without waiting the end of the statement evaluation, do you confirm this thesis?
Thanks in advance.

The_Nick

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

Posted: Thu Mar 07, 2013 7:08 am
by admin
The_Nick wrote:It must mean that the method getH() gets executed straight away without waiting the end of the statement evaluation, do you confirm this thesis?
Thanks in advance.

The_Nick
Yes.

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

Posted: Mon Sep 29, 2014 3:32 pm
by jbilkes
This concept of first 'calculating' String-values by executing all methods inside is totally new to me. It should definitely be mentioned in the answer because its a) important and b) never mentioned before as far as I know, thx

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

Posted: Mon Sep 29, 2014 8:11 pm
by admin
Not sure what you mean. There is nothing new in the question with respect to printing Strings using System.out.println.
Concatenating Strings is covered in detail in String related questions.

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

Posted: Sun Dec 07, 2014 4:41 pm
by gparLondon
I executed this code. The o/p is

Beta 44 4 44
Beta 44 44 44.

Which was the same as answer given by the author. But, my question is, why the answer is not

4 Beta 44 44
44 Beta 44 44?
:roll:
Why is it printing a String first than int? as in println of main first statement is b.h?
I may be asking the same question asked by many, but I am asking this, because none of the answers given above are clear to me.

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

Posted: Sun Dec 07, 2014 8:37 pm
by admin
No, the first print statement is not just b.h. It is: b.h+" "+b.getH()
Before the println method can be executed, its argument has to be evaluated. So it has to first compute the value of b.h+" "+b.getH(). To compute its value, it needs to call b.getH(). And this call causes other println() to execute.

HTH,
Paul.

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

Posted: Sun Jan 04, 2015 5:36 am
by disznoperzselo
Next time somebody says that "OCAJP is easy" I will simply train my gun on them like Samuel L. Jackson in Pulp Fiction and say that "Say OCAJP is EASY one more time"! ... And then I will ask them this easy question and see how they perform under pressure :) I hope they will understand at last why passing OCAJP is hard indeed ..