About Question enthuware.ocajp.i.v8.2.1482 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by admin »

asenevtimov wrote:Hi all, please someone explain why bb.getH() method calls the the method getH() from the Baap class. bb refer to an object of class Beata, so it should call Beata's getH(). Which overridden method is called depends on the object type isn't it?
Yes, bb.getH() does indeed call Beta's getH. Why do you think it calls Baap's getH?
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by Javier »

Hi!!

I would like to know why the statement:

System.out.println(b.h+" "+b.getH());

is not printing:

Beta 44
4 Beta 44 //Because b.getH() is saying to print "Beta 44".

Why the first time calling b.getH() is printing "Beta 44" and the second line is just returning the h value?

Thank you very much Enthuware!

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

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by admin »

The statement System.out.println(b.h+" "+b.getH()); will print whatever string is computed by combining b.h , " " , and the value returned by calling b.getH(). So to compute that value, it has to first call b.getH(). In the call to b.getH(), there is another System.out.println, which prints "Beta "+h, which prints "Beta 44" and it returns the value of h i.e. 44.

Now, the value of the first println statement can be computed i.e. 4+" "+44, so "4 44" is printed.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1482 :

Post by Javier »

Great explanation!!

Thank you very much guys!!

Post Reply

Who is online

Users browsing this forum: No registered users and 50 guests