Hi there,
I am trying to understand the code from Question 63, Test 1 of your IZ0-808 mock exam.
A few lines from the code:
public static void main(String[] args) {
Baap b = new Beta();
System.out.println(b.h+ " " + b.getH()); //(A), I am stuck at this line, see question below
//Beta bb = (Beta) b; //i commentend these out
//System.out.println(bb.h + " " + bb.getH());
}
The output in IntelliJ are:
Beta 44
4 44
The class code is:
public class Baap {
public int h = 4;
public int getH() {
System.out.println("Baap " + h);
return h; }
}
public class Baap {
public int h = 4;
public int getH() {
System.out.println("Baap " + h);
return h; }
}
Question: At (A) above, the 1st call is to b.h. The variable b is referring to the variable from class Baap and in Baap the h variable has a value of 4. Why is 4 not being the 1st value printed? Instead 'Beta' from the b.getH() method is the 1st value printed in the output.
Thanks,
Daniel
Mock exam question
Moderator: admin
-
- Posts: 2
- Joined: Sun Aug 23, 2020 1:27 pm
- Contact:
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Mock exam question
This question has been discussed multiple times in detail. Please go through the following discussions and let me know if you still have doubts:
viewtopic.php?f=2&t=3214
and
viewtopic.php?f=2&t=2075
viewtopic.php?f=2&t=3214
and
viewtopic.php?f=2&t=2075
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 106 guests