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.
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
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.
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
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.
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]
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.
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..
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.