About Question enthuware.ocajp.i.v7.2.1288 :
Moderator: admin
-
- Posts: 3
- Joined: Sun Jun 03, 2012 5:45 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1288 :
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()?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1288 :
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.
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.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1288 :
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.
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.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1288 :
This is what happens when the first SOP in main ( System.out.println(b.h+" "+b.getH()); )executes: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.
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.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1288 :
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().
Respectfully,
Javanaut
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().
Respectfully,
Javanaut
Re: About Question enthuware.ocajp.i.v7.2.1288 :
Could you please explain why following code not function in same manner?
Here output is 8 Accelerate : SportsCar
It prints the gearRatio value from the parent class
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() );
}
}
It prints the gearRatio value from the parent class
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1288 :
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.Guest wrote:Could you please explain why following code not function in same manner?
Here output is 8 Accelerate : SportsCarCode: 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() ); } }
It prints the gearRatio value from the parent class
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Tue Feb 12, 2013 9:12 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1288 :
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 :
So the printout should be
for b.h (4 Beta 44 44) &
for bb.h (44 Beta 44 44)?
for b.h (4 Beta 44 44) &
for bb.h (44 Beta 44 44)?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1288 :
Yes.AlsoConfused.java wrote:So the printout should be
for b.h (4 Beta 44 44) &
for bb.h (44 Beta 44 44)?
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1288 :
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
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 :
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
Thanks in advance.
The_Nick
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1288 :
Yes.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
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Tue Sep 09, 2014 3:28 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1288 :
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
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1288 :
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.
Concatenating Strings is covered in detail in String related questions.
If you like our products and services, please help us by posting your review here.
-
- Posts: 63
- Joined: Fri Oct 31, 2014 6:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1288 :
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?
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.
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?
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.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1288 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Fri Jan 02, 2015 12:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1288 :
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 ..
Who is online
Users browsing this forum: No registered users and 112 guests