Page 1 of 2
About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Wed Feb 26, 2014 7:44 am
by cesar.alcancio
Is that compiler error?
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Wed Feb 26, 2014 11:00 am
by admin
No, why do you think so?
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Sun May 04, 2014 6:12 am
by bolo.olbo
Compilation error is wrong, appropriate answer should be: 30,40. It is bug in test.
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Sun May 04, 2014 7:49 am
by admin
30, 40 is indeed marked as correct.
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Tue May 13, 2014 9:16 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Tue May 13, 2014 9:37 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Wed May 14, 2014 7:14 am
by anderijk1
Thanks Paul that makes sense to me now.
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Thu Apr 07, 2016 1:24 pm
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
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Thu Apr 07, 2016 9:17 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Thu Nov 17, 2016 9:01 am
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?
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Thu Nov 17, 2016 10:34 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Wed Dec 14, 2016 11:12 pm
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
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Thu Jan 26, 2017 4:35 am
by Gagandeep
I am getting Compilation error on this code. Its due to
as variable y is declared as static in class MNOP.
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Thu Jan 26, 2017 5:59 am
by admin
There is an extra bracket in your code.
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Wed Jul 05, 2017 8:27 pm
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?
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Wed Jul 05, 2017 9:31 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Thu Jul 06, 2017 10:11 am
by mjmsausava
Got it. thanks.
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Mon Sep 11, 2017 5:23 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Mon Sep 11, 2017 11:07 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Thu Feb 08, 2018 11:52 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Thu Feb 08, 2018 1:04 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Fri Feb 09, 2018 4:09 pm
by ironicsushi
Thank you!
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Tue Apr 17, 2018 7:12 am
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..
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Tue Apr 17, 2018 8:47 am
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
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Posted: Sun Feb 24, 2019 6:47 pm
by OlgaAG
the question removed as unnecessary