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

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

Moderator: admin

Chandu
Posts: 7
Joined: Thu Mar 30, 2017 7:15 am
Contact:

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

Post by Chandu »

Code: Select all

class A{
   A() {  print();   }
   void print() { System.out.println("A"); }
}
class B extends A{
   int i =   4;
   public static void main(String[] args){
      A a = new B();
      a.print();
   }
   void print() { System.out.println(i); }
}
What will be the output when class B is run ?

Explanation: Note that method print() is overridden in class B. Due to polymorphism, the method to be executed is selected depending on the class of the actual object. Here, when an object of class B is created, first B's default constructor (which is not visible in the code but is automatically provided by the compiler because B does not define any constructor explicitly) is called. The first line of this constructor is a call to super(), which invokes A's constructor. A's constructor in turn calls print(). Now, print is a non-private instance method and is therefore polymorphic, which means, the selection of the method to be executed depends on the class of actual object on which it is invoked. Here, since the class of actual object is B, B's print is selected instead of A's print. At this point of time, variable i has not been initialized (because we are still in the middle of initializing A), so its default value i.e. 0 is printed.   Finally, 4 is printed. 
Query-------------// in this i got a doubt that if value of "i" isnt initialized at that point of time then how come its assigning the default int value for that variable? i mean if the control doesnt go to that line of code during that particular time of execution then how come compiler knows the data type of that variable and assigns the default value for it?

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

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

Post by admin »

1. This is about execution. There is no compiler involved in execution. It is the JVM.
2. When an object is created, the first thing JVM does is allocate space for that object. This space includes space for its instance variables. This space that is used for instance variables is all filled with zeros. This is the default value of all kinds of variables. If the variable is a primitive it is 0, if it is a boolean the value is interpreted as false, and if the variable is a reference variable, it is interpreted as null.
3. After the JVM allocates space for an object, it executes the initializers, and constructors.

The value that is being talked about in this question is the value that is there in that space after memory allocation and before execution of any initializers/constructors.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

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

Post by shambhavi »

gustavomzina wrote:I understand that instance members are initialized after the super() method call within B class constructor. But why does the code below outputs 4, 4?

Code: Select all

class A{
	A() {  print();   }
	void print() { System.out.println("A"); }
}
public class B extends A{
	final int i =   4;
	public static void main(String[] args){
		A a = new Teste();
		a.print();
	}
	void print() { System.out.println(i); }
}
Final members are not instance members?

Thanks.
Final fields are instance members but they are not always given a default value ! try it out as illustrated in the explanations below !
int i; will compile but
final int i; // will not compile if you haven't initialized it anywhere in the constructor too

since in your code you have given final int i=4; this is a compile time constant whose value is known at compile time itself and hence, when this variable is created at run time it will be assigned this value itself rather than a default value because anyways its a constant and its value will not change in the rest of the program !

but observe in the below code : output is :
0
4

Code: Select all

class A{
	A() {  print();   }
	void print() { System.out.println("A"); }
}
 public class B extends A {
    
final int i;
B()
{
    i=4;
}

   void print() { System.out.println(i); } 
     
           
  public static void main(String[] args) { 
    
  A a = new B();
		a.print();
 
      System.out.println();
      
      
 }
 }
here a default value is given since its value is not known at compile time and 4 is assigned only after its first useage !

Angi000
Posts: 3
Joined: Sun Jan 28, 2018 12:44 pm
Contact:

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

Post by Angi000 »

Hi Paul,

I don't understand why the default value of 'i' is used when creating the object 'a' is referring to. So 0, not 4. Please help.

Thanks

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

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

Post by admin »

When you do new B(), the JVM has to initialize the memory space for an object of class B. Included in B are the instance fields defined in A as well. So when the JVM tries to initialize B, it has to first initialize the instance members of A. While initializing the fields of A, the JVM encounters a print statement that tries to print the value of i. But at this time, i has not been initialized fully (because B's constructor hasn't run yet), so the print statement prints the default value assigned by the JVM i.e. 0.
If you like our products and services, please help us by posting your review here.

ArpRokz
Posts: 15
Joined: Sun Jan 28, 2018 12:38 pm
Contact:

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

Post by ArpRokz »

I understood this in the following way..

When we call print() from within class A's constructor it is like calling this.print(). Here "this" refers to the currently calling object. Since the actual object used was of class B therefore class B's print() is called. Now since class B's constructor requires a call to class A constructor first so i=4 is not executed till now therefore 0 is printed.

Is this explanation correct or am I wrong ?

elias86
Posts: 7
Joined: Fri May 04, 2018 4:14 am
Contact:

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

Post by elias86 »

Why is printed 0 instead of A???

When I call the B constructor (which is a default constructor because it's not declared) java calls the super-constructor (A constructor) so it calls print method (of the B class). This method calls print() method that prints i. So if i == 4 why it prints 0?

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

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

Post by admin »

Please go through the discussion above.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post by crazymind »

Hi, I read your explanation of this question; However, there is one thing still confuse me. According to order of initialization, the instance variable initializes before the constructor call. So before calls A(), 'i' is initialized to 4 already. Why is it 0? thanks

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

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

Post by admin »

Please go through the discussion above.
If you like our products and services, please help us by posting your review here.

Denyo1986
Posts: 38
Joined: Thu Jan 07, 2021 2:47 am
Contact:

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

Post by Denyo1986 »

I dont mean to be a smart-ass but since in other questions it was more precise and especially since there is a "none of the above"-option, I would suggest to remove the comma from the response options, as there is no piece of code printing any comma. The correct output would be 0 4.

Great question by the way. I would never have imagined in my wildest dreams that A calls the overridden method. I would have thought it always calls its own method. This is crazy....

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

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

Post by admin »

Good point. Done.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

fortesp
Posts: 9
Joined: Mon Dec 14, 2020 8:53 am
Contact:

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

Post by fortesp »

Shouldn't this be "None of the above"? The result after executing the code is 04 and not 0 4.

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

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

Post by admin »

You are right. Updated System.out.print(i); to System.out.print(i+" ");
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 43 guests