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

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

Moderator: admin

cesar.alcancio
Posts: 1
Joined: Wed Feb 26, 2014 7:43 am
Contact:

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

Post by cesar.alcancio »

Is that compiler error?

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

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

Post by admin »

No, why do you think so?
If you like our products and services, please help us by posting your review here.

bolo.olbo
Posts: 1
Joined: Sun May 04, 2014 6:08 am
Contact:

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

Post by bolo.olbo »

Compilation error is wrong, appropriate answer should be: 30,40. It is bug in test.

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

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

Post by admin »

30, 40 is indeed marked as correct.
If you like our products and services, please help us by posting your review here.

anderijk1
Posts: 4
Joined: Sat Mar 22, 2014 11:33 am
Contact:

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

Post by anderijk1 »

Can you please explain the difference between (1) System.out.println(new MNOP().x) and  (2) ABCD a = new MNOP(); System.out.println(a.x); I have never seen (1) before and I'm confused since it appears to be creating an instance of the object. I understand (2) and shadowing but I don't understand why (1) wouldn't also be shadowing.

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

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

Post by admin »

new MNOP().x is just a short form for the following two lines:

MNOP temp = new MNOP();
temp.x;

Of course, there is no actual variable named temp but the JVM does actually create a temporary reference and then accesses x on that reference. You can now see that MNOP's x (and not ABCD's x) will be accessed because the type of x is MNOP and not ABCD.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

anderijk1
Posts: 4
Joined: Sat Mar 22, 2014 11:33 am
Contact:

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

Post by anderijk1 »

Thanks Paul that makes sense to me now.

kirankumar
Posts: 5
Joined: Tue Apr 05, 2016 10:25 am
Contact:

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

Post by kirankumar »

Is it possible to override instance variables? I am pretty sure about we can't override static variables but little confusing about the instance variables

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

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

Post by admin »

Variables (static or instance) are never overridden. They can be hidden (when you have a field with the same name in super class and subclass).
The concept of overriding applies only to instance methods.
-Paul.
If you like our products and services, please help us by posting your review here.

vlezz94
Posts: 12
Joined: Wed Sep 28, 2016 6:31 am
Contact:

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

Post by vlezz94 »

Hi!

I'm struggling to understand de concept of reference and type inside a declaration. Let's suppose I have this classes:

Code: Select all

Class A {
        int a = 0;
}

Class B {
        int a = 1;
}

Class App{

        public static void main (String[] args){
              A a = new B();
              System.out.printl(a.a); // The output is: 0

}
So, when we instantiate an Object the value we get is from the reference type (A) and not from the Object reference (B). Am I correct?

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

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

Post by admin »

vlezz94 wrote:So, when we instantiate an Object the value we get is from the reference type (A) and not from the Object reference (B). Am I correct?
Yes, that is correct. In Java, only the method calls are polymorphic. That means, if you call a method on a reference of type A, the method of the class of the actual object to which a is pointing (here, B) will be called. Field access is not polymorphic. It depends on the type of the reference and not of the object.

BTW, your code A a = new B() will not compile because classes B and A are totally unrelated. There is no way a reference of type A can point to an object of type B so the compiler will not accept it.

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

JavaJunky
Posts: 4
Joined: Wed Dec 14, 2016 11:07 pm
Contact:

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

Post by JavaJunky »

vlezz94 wrote:Hi!

I'm struggling to understand de concept of reference and type inside a declaration. Let's suppose I have this classes:

Code: Select all

Class A {
        int a = 0;
}

Class B {
        int a = 1;
}

Class App{

        public static void main (String[] args){
              A a = new B();
              System.out.printl(a.a); // The output is: 0

}
So, when we instantiate an Object the value we get is from the reference type (A) and not from the Object reference (B). Am I correct?
In this case Yes. But if you have method instead of instance/static variable then it you get the value from B

Gagandeep
Posts: 1
Joined: Thu Jan 26, 2017 4:32 am
Contact:

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

Post by Gagandeep »

I am getting Compilation error on this code. Its due to

Code: Select all

new MNOP().y);
as variable y is declared as static in class MNOP.

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

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

Post by admin »

There is an extra bracket in your code.
If you like our products and services, please help us by posting your review here.

mjmsausava
Posts: 19
Joined: Sat Mar 25, 2017 5:38 am
Contact:

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

Post by mjmsausava »

This line in the question:
System.out.println(new MNOP().x+", "+new MNOP().y);

y is a static variable. I thought static variables can't be accessed by prefixing an object (new MNOP().y). Or that rule is only for static methods?

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

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

Post by admin »

mjmsausava wrote: I thought static variables can't be accessed by prefixing an object (new MNOP().y). Or that rule is only for static methods?
No, the rule is that static members (i.e. fields as well as methods) don't need any object reference to be accessed. They need only class name. (Or not even that if you are accessing them from the same class).

However, it is allowed to use any object reference (instead of a class name) as well to access them because the compiler can always figure out the class name of any reference variable. That is why new MNOP().y is valid.

Instance members, on the other hand, can only be accessed through a reference to an object. If you don't use any reference, the compiler will try applying the "this" implicit reference to access the member.
If you like our products and services, please help us by posting your review here.

mjmsausava
Posts: 19
Joined: Sat Mar 25, 2017 5:38 am
Contact:

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

Post by mjmsausava »

Got it. thanks.

sgshankar
Posts: 1
Joined: Mon Sep 11, 2017 5:17 pm
Contact:

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

Post by sgshankar »

Code: Select all

class ABCD {
	int x = 10;
	static int y = 20;
	
	public Object getValue() {
		return new Object();
	} //
}

class MNOP extends ABCD {
	int x = 30;
	static int y = 40;
	
	public String getValue() {
		return "hello";
	} 
}

public class TestClass12 {
	public static void main(String[] args) {
		
		ABCD a = new MNOP();
		
		
		System.out.println("a.x - " + a.x + " a.y - " + a.y);
		
		System.out.println(a.getValue());
	}
}
-----------------------
Output -

a.x - 10 a.y - 20
hello
-----------------------

Seems I am missing something here, pls help me understand this.

ABCD a = new MNOP(); --> now a refers to the child class MNOP and a.getValue() calls the method in class MNOP.

But a.x and a.y are printing values x = 10 and y = 20 from Super Class ABCD.

Can someone explain why it is not printing the values x= 30, and y = 40 from the sub class MNOP.
Last edited by admin on Mon Sep 11, 2017 10:56 pm, edited 1 time in total.
Reason: Please enter code inside [code] [/code] tags

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

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

Post by admin »

Did you read the explanation? It explains exactly what you are asking.
Access to static and instance fields and static methods depends on the class of reference variable and not the actual object to which the variable points to. Observe that this is opposite of what happens in the case of instance methods. In case of instance methods the method of the actual class of the object is called.
If you like our products and services, please help us by posting your review here.

ironicsushi
Posts: 2
Joined: Thu Feb 08, 2018 11:47 am
Contact:

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

Post by ironicsushi »

I am very confused. I ran this in a compiler online:

Code: Select all

class ABCD{
   int x = 10;
   static int y = 20;
}
class MNOP extends ABCD{
   int x = 30;
   static int y = 40;
}

public class TestClass {
   public static void main(String[] args) {
     System.out.println(new MNOP().x+", "+new MNOP().y);
     
     ABCD a = new MNOP();
     System.out.println(a.x);
     System.out.println(a.y);
   }
}
I got:

30, 40
10
20

I predicted that the bottom block of code would print 30 and 20, not 10 and 20. I thought that static variables belong to the class, so it makes sense that a.y would print 20.

But I thought instance methods would call the method on the actual object, not the type of the reference... so isn't the actual object MNOP, therefore shouldn't a.x be 30, not 10?

I hope someone can clarify this issue for me.
Last edited by admin on Thu Feb 08, 2018 12:54 pm, edited 1 time in total.
Reason: Please put code inside [code] [/code]

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

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

Post by admin »

It is true that static variables belong to the class. But that has nothing to do this.
a is reference of type ABCD. Yes, a refers to an object of type MNOP, but the declared type of a is ABCD and the compiler only checks the declared type of the reference variable to determine which variable it needs to use.

Compiler does not know the type of the actual object that will be referred to by a variable. Only a JVM can know that. In case of instance methods, the compiler defers this decision to the JVM and that is why methods are polymorphic. In case of variables and static methods, the compiler makes the decision by itself just by looking at the type of the variable.

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

ironicsushi
Posts: 2
Joined: Thu Feb 08, 2018 11:47 am
Contact:

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

Post by ironicsushi »

Thank you!

cknoxo
Posts: 1
Joined: Tue Apr 17, 2018 7:07 am
Contact:

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

Post by cknoxo »

I take it when they show these classes together, they aren't to be thought of as compiled in the same .java class?
Because this would throw a compile error..
Kind of sneaky unless it says somewhere that "anytime a question shows multiple classes in the one code print, consider it as a different .java file for each class"
If you put all the code shown into one .java file it wont like the int x in MNOP as its the same name as the one in ABCD. If you made the x in ABCD private, it would fix the issue. So i put compilation error and got it wrong..

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

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

Post by admin »

Yes, Oracle has mentioned this on their official 1z0-808 certification details page:
No file or directory path names for classes: If a question does not state the file names or directory locations of classes, then assume one of the following, whichever will enable the code to compile and run:
All classes are in one file
Each class is contained in a separate file, and all files are in one directory
If you like our products and services, please help us by posting your review here.

OlgaAG
Posts: 10
Joined: Thu Jan 03, 2019 3:08 pm
Contact:

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

Post by OlgaAG »

the question removed as unnecessary
Last edited by OlgaAG on Mon Mar 11, 2019 2:09 am, edited 1 time in total.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 45 guests