Page 1 of 1

About Question enthuware.ocpjp.v11.2.1890 :

Posted: Sun Jun 06, 2021 2:22 pm
by stanmak
class Baap {
public int h = 4;

public int getH() {
System.out.println("Baap " + h);
return 5;
}
}

public class Main2 extends Baap {
public int h = 44;

public int getH() {
System.out.println("Beta " + h);
return h;
}


public static void main(String[] args) {
Main2 b = new Main2();
System.out.println(b.h + " " + b.getH());
Main2 bb = (Main2) b;
System.out.println(bb.h + " " + bb.getH()); }

}
//output
Beta 44
44 44
Beta 44
44 44

//Question
I dont understand why it System.out.println(b.h + " " + b.getH()); didtnt print 44 Beta 44 . I think b.h should have resulted to 44. Then b.getH prints Beta then 44 so that we have 44 Beta 44 as opposed to Beta 44

Re: About Question enthuware.ocpjp.v11.2.1890 :

Posted: Sun Jun 06, 2021 4:08 pm
by admin
Explained in this thread viewtopic.php?f=2&t=2075