About Question enthuware.ocajp.i.v7.2.1320 :
Moderator: admin
-
- Posts: 1
- Joined: Wed Feb 26, 2014 7:43 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1320 :
Is that compiler error?
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
No, why do you think so?
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Sun May 04, 2014 6:08 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Compilation error is wrong, appropriate answer should be: 30,40. It is bug in test.
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
30, 40 is indeed marked as correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Sat Mar 22, 2014 11:33 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
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.
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
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.
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.
-
- Posts: 4
- Joined: Sat Mar 22, 2014 11:33 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Thanks Paul that makes sense to me now.
-
- Posts: 5
- Joined: Tue Apr 05, 2016 10:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
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
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
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.
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.
-
- Posts: 12
- Joined: Wed Sep 28, 2016 6:31 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Hi!
I'm struggling to understand de concept of reference and type inside a declaration. Let's suppose I have this classes:
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?
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
}
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
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.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?
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.
-
- Posts: 4
- Joined: Wed Dec 14, 2016 11:07 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
In this case Yes. But if you have method instead of instance/static variable then it you get the value from Bvlezz94 wrote:Hi!
I'm struggling to understand de concept of reference and type inside a declaration. Let's suppose I have this classes:
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?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 }
-
- Posts: 1
- Joined: Thu Jan 26, 2017 4:32 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
I am getting Compilation error on this code. Its due to
as variable y is declared as static in class MNOP.
Code: Select all
new MNOP().y);
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
There is an extra bracket in your code.
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Sat Mar 25, 2017 5:38 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
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?
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?
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
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).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?
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.
-
- Posts: 19
- Joined: Sat Mar 25, 2017 5:38 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
Got it. thanks.
-
- Posts: 1
- Joined: Mon Sep 11, 2017 5:17 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
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
Reason: Please enter code inside [code] [/code] tags
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
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.
-
- Posts: 2
- Joined: Thu Feb 08, 2018 11:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
I am very confused. I ran this in a compiler online:
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.
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);
}
}
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]
Reason: Please put code inside [code] [/code]
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
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.
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.
-
- Posts: 2
- Joined: Thu Feb 08, 2018 11:47 am
- Contact:
-
- Posts: 1
- Joined: Tue Apr 17, 2018 7:07 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
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..
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..
-
- Site Admin
- Posts: 10067
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
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.
-
- Posts: 10
- Joined: Thu Jan 03, 2019 3:08 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1320 :
the question removed as unnecessary
Last edited by OlgaAG on Mon Mar 11, 2019 2:09 am, edited 1 time in total.
Who is online
Users browsing this forum: No registered users and 219 guests