Question relating to mixing up the overriding of stating and instance variables? - java

Ttaking the following code, (source):
class Parent {
Integer a = 1;
static Integer b = 2;
}
class Child extends Parent {
static Integer a = 41;
Integer b = 42;
}
public class Test {
public static void main(String[] args) {
Parent parent = new Parent();
Child child = new Child();
Parent yo = new Child();
System.out.format("%d %d %d %d %d %d ",
parent.a,
parent.b,
child.a,
child.b,
yo.a,
yo.b);
}
}
Why is the result of yo.a and yo.b 1 and 2 respectively? I am confused since, yo points to a Child object, it would yield 41 and 2 as the result, since in Parent, a is non-static, thus the subclass's version of a will be printed instead of the superclass version.

Variables (fields) are not overridden, neither instance variables nor class variables.
An object always has all the instance variables from all the superclasses. A class has only the static variables it defines itself, though it also can access the superclasses' (and interfaces') variables (if not private).
If you redefine a variable in a subclass, you are shadowing the original one, i.e. can't directly access it. It is still existent.
Which variable is accessible only depends on the (compile-time) type used to access it, not on the concrete object's class.

There is no dynamic binding for fields, all bindings for fields are done at compile time. That is why , it is printed fields of parent class not child. And also, static modifier does not change any thing, you can remove all static modifier and also you get the same result..
You should keep in mind that, only instance methods are dynamically binded in Java language.

Maybe this modified example helps to clarify the decision on which fields are seen or hidden, based on the compile-time type:
public static void main(String[] args) {
Parent parent = new Parent();
Child child = new Child();
Parent yo = new Child();
System.out.println(parent.a + " " + parent.b);
System.out.println(child.a + " " + child.b);
// you said it's a Parent
System.out.println(yo.a + " " + yo.b);
// but now you're saying it's a child
System.out.println(((Child)yo).a + " " + ((Child)yo).b);
// you said Child, but now you're saying it's a Parent
System.out.println(((Parent)child).a + " " + ((Parent)child).b);
}
which gives output:
1 2
41 42
1 2
41 42
1 2
The compiler is deciding based on the information you give it.

There is no instance variable a in class Child objects. Or, more precisely, the only instance variable a in Child objects is the one inherited from Parent; class Child does not define its own instance variable named a.

Classes have fields. Instance fields cannot be overridden, only hidden in sub-classes. Local variables cannot be overridden or hidden. Static fields and method are bound to the class and attempting to use an instance variable is misleading because the instance variable is ignored.
e.g.
Parent yo = null;
System.out.println(yo.a); // prints 1
BTW: It is better to use int instead of Integer unless you really need an Integer.

Related

What happens to variables/objects in inner classes of function objects?

I have a function multi2 which returns inner class Inner as an Object.
What happens to a - where is it saved and how can I access it?
public class C {
private static Object multi2(final int a) {
class Inner {
public int hashCode() {
return 2*a;
}
}
return new Inner(); // What happens to a?
// Who allocates a?
// Can I Access a?
}
public static void main(String[] args) {
Object o = multi2(6);
System.out.println("o.hashCode() = " + o.hashCode());
o = multi2(4);
System.out.println("o.hashCode() = " + o.hashCode());
}
}
What happens at the implementation level is that a copy of the value of a is saved in a synthetic instance variable declared in the compiled version of the C.Inner class.
The value of a is passed to the compiled Inner constructor via an extra parameter.
The C.Inner.hashCode method uses the value of the synthetic variable. Accessing a in the source code of Inner.hashCode is transformed into accessing the corresponding synthetic variable in the compiled code.
The variable in the outer scope must be final1. The synthetic variable must be final2 in the Inner class. This maintains the illusion that (potentially) multiple instances of the Inner class are seeing the same a variable. (They aren't, but since the variable(s) can't be changed, it is not possible for the code of the inner class to tell the difference.)
If you use javap to look at the bytecodes for the compiled example, you will see the mechanisms used to implement this in the outer and the inner classes.
1 - or effectively final from Java 8 onwards.
2 - If a could be mutated by an Inner method, then two Inner instances with the same outer class need to share a mutable variable whose lifetime is (now) longer than the stackframe for a multi2 call. That entails somehow turning a from stack variable into something that lives on the heap. It would be expensive and complicated.
You have defined the class Inner inside the function so the scope of the class will be
restricted with in the method. And your function is static so it will be live as long as the class definition is loaded. You have override the hashCode function inside the InnerClass so every time you are calling the multi2(param) you are creating the hashCode for the instance of InnerClass and returning the instance of the InnerClass.
So as for you questions, please correct me if i am wrong.
What happens to a ?
a is with in the scope of your static method, so it will be live as long as the class definition is loaded.
Who allocates a?
scope of a is restricted inside the static method and static method does not require instance to access it but as for the static method/variable allocation, i think it depends on JVM.
Can I Access a?
No you cannot access a from outside you static method, it is restricted with in your static method.
Since the "a" is a local parameter, you could use a different approach to read the "a" value:
public class C {
public static Object multi2(final int a) {
return new Inner(a);
}
public static void main(String[] args) {
Object o = multi2(6);
System.out.println("o.hashCode() = " + o.hashCode());
System.out.println("o.getA() = " + ((Inner) o).getA());
o = multi2(4);
System.out.println("o.hashCode() = " + o.hashCode());
System.out.println("o.getA() = " + ((Inner) o).getA());
}
}
class Inner{
public int valueA;
public Inner(int a)
{
valueA = a;
}
public int getA() {
return valueA;
}
public int hashCode() {
return 2*valueA;
}
}
I wanted to know what was actually happening, so I compiled your code and looked at the bytecode output.
Basically what happens is the compiler adds in a constructor to your class 'Inner'. It also adds a single parameter to that constructor which takes 'a'. If your multi2() method was NOT static then there would probably also be a parameter to take 'this' where 'this' is the instance of 'C' that multi2() is executing on. BUT since we're in static context, there is no 'this'.
The compiler adds a private final field to your class 'Inner' and sets that private field using the value passed via the constructor. The compiler also converts
new Inner()
into
new Inner(a)
Hashcode then accesses the private field containing the value for a.
If 'a' was an object instead of a primitive, then it would be the same way, but a reference would be passed through instead of an actual number value.
How do you access this variable? Well you access it with reflections, but there are many problems:
1) You don't know the name of the field made by the compiler, so you can only get the name by looking at the bytecode. Don't trust decompilers as they might change the name. You gotta look at the bytecode yourself to find out.
2) The compiler probably marks the field as final, which means even if you can get reflections to access the field for you, you won't be able to update it.
3) It is entirely up to the compiler to figure out field names. Field names could change between builds depending on the compiler and it's mood.
Inner is a so called local class. a is a parameter passed to the method multi2 and accessable within that scope. Outside of that method, you cannot access a.

Objects With Multiple Fields of the Same Name [duplicate]

I am studying overriding member functions in Java and thought about experimenting with overriding member variables.
So, I defined classes
public class A{
public int intVal = 1;
public void identifyClass()
{
System.out.println("I am class A");
}
}
public class B extends A
{
public int intVal = 2;
public void identifyClass()
{
System.out.println("I am class B");
}
}
public class mainClass
{
public static void main(String [] args)
{
A a = new A();
B b = new B();
A aRef;
aRef = a;
System.out.println(aRef.intVal);
aRef.identifyClass();
aRef = b;
System.out.println(aRef.intVal);
aRef.identifyClass();
}
}
The output is:
1
I am class A
1
I am class B
I am not able to understand why when aRef is set to b intVal is still of class A?
When you make a variable of the same name in a subclass, that's called hiding. The resulting subclass will now have both properties. You can access the one from the superclass with super.var or ((SuperClass)this).var. The variables don't even have to be of the same type; they are just two variables sharing a name, much like two overloaded methods.
Variables are not polymorphic in Java; they do not override one another.
There is no polymorphism for fields in Java.
Variables decision happens at a compile time so always Base Class variables (not child’s inherited variables) will be accessed.
So whenever upcasting happens always remember
1) Base Class variables will be accessed.
2) Sub Class methods(overridden methods if overriding happened else inherited methods as it is from parent) will be called.
Variables are resolved compile-time, methods run-time. The aRef is of type A, therefore aRef.Intvalue is compile-time resolved to 1.
OverRiding Concept in Java
Functions will override depends on object type and variables will accessed on reference type.
Override Function: In this case suppose a parent and child class both have same name of function with own definition. But which function will execute it depends on object type not on reference type on run time.
For e.g.:
Parent parent=new Child();
parent.behaviour();
Here parent is a reference of Parent class but holds an object of Child Class so that's why Child class function will be called in that case.
Child child=new Child();
child.behaviour();
Here child holds an object of Child Class, so the Child class function will be called.
Parent parent=new Parent();
parent.behaviour();
Here parent holds the object of Parent Class, so the Parent class function will be called.
Override Variable: Java supports overloaded variables. But actually these are two different variables with same name, one in the parent class and one in the child class. And both variables can be either of the same datatype or different.
When you trying to access the variable, it depends on the reference type object, not the object type.
For e.g.:
Parent parent=new Child();
System.out.println(parent.state);
The reference type is Parent so the Parent class variable is accessed, not the Child class variable.
Child child=new Child();
System.out.println(child.state);
Here the reference type is Child, so the Child class variable is accessed not the Parent class variable.
Parent parent=new Parent();
System.out.println(parent.state);
Here the reference type is Parent, so Parent class variable is accessed.
From JLS Java SE 7 Edition §15.11.1:
This lack of dynamic lookup for field accesses allows programs to be run efficiently with straightforward implementations. The power of late binding and overriding is available, but only when instance methods are used.
Answers from Oliver Charlesworth and Marko Topolnik are correct, I would like to elaborate a little bit more on the why part of the question:
In Java class members are accessed according the type of the reference and not the type of the actual object. For the same reason, if you had a someOtherMethodInB() in class B, you wouldn't be able to access it from aRef after aRef = b is run. Identifiers (ie class, variable, etc names) are resolved at compile time and thus the compiler relies on the reference type to do this.
Now in your example, when running System.out.println(aRef.intVal); it prints the value of intVal defined in A because this is the type of the reference you use to access it. The compiler sees that aRef is of type A and that's the intVal it will access. Don't forget that you have both fields in the instances of B. JLS also has an example similar to yours, "15.11.1-1. Static Binding for Field Access" if you want to take a look.
But why do methods behave differently? The answer is that for methods, Java uses late binding. That means that at compile time, it finds the most suitable method to search for during the runtime. The search involves the case of the method being overridden in some class.
I hope this can help:
public class B extends A {
// public int intVal = 2;
public B() {
super();
super.intVal = 2;
}
public void identifyClass() {
System.out.println("I am class B");
}
}
So overriding variable of base class is not possible, but base class variable value can be set (changed) from constructor of inherited class.
This is called variable hiding. When you assign aRef = b; , aRef has two intVal, 1 is named just intVal another is hidden under A.intVal (see debugger screenshot), Because your variable is of type class A , even when you print just intVal java intelligently picks up A.intVal.
Answer 1: One way of accessing child class's intVal is System.out.println((B)aRef.intVal);
Answer 2: Another way of doing it is Java Reflection because when you use reflection java cant intelligently pickup hidden A.intVal based on Class type, it has to pick up the variable name given as string -
import java.lang.reflect.Field;
class A{
public int intVal = 1;
public void identifyClass()
{
System.out.println("I am class A");
}
}
class B extends A
{
public int intVal = 2;
public void identifyClass()
{
System.out.println("I am class B");
}
}
public class Main
{
public static void main(String [] args) throws Exception
{
A a = new A();
B b = new B();
A aRef;
aRef = a;
System.out.println(aRef.intVal);
aRef.identifyClass();
aRef = b;
Field xField = aRef.getClass().getField("intVal");
System.out.println(xField.get(aRef));
aRef.identifyClass();
}
}
Output -
1
I am class A
2
I am class B
Well, I hope u got the answer. If not, you can try seeing in the debug mode. the subclass B has access to both the intVal. They are not polymorphic hence they are not overriden.
If you use B's reference you will get B's intVal. If you use A's reference , you will get A's intVal. It's that simple.
As per the Java specifications, the instance variables are not overridden from a super class by a sub class when it is extended.
Hence the variable in the sub class only can be seen as one sharing the same name.
Also when the constructor of A is called during the instance creation of B the variable (intVal) is initialized and hence the output.
It is because when you assign b to aRef, it is resolved, leading aRef to just be of class A. This means that aRef does not have access to any of class B's fields or methods. If you call for intVal instead by using b.intVal, you will get 2.
Java has a feather of encapsulation means it tightly binds the property and the behavior of an object. so only via a class reference we can call it's behavior to change it's property.
and in inheritance only method overrides so that it can affects only it's property.
As Many users have already pointed out, this is not polymorphism. Polymorphism only applies to methods(functions).
Now as to why the value of the intVal of class A is printed, this happens because as you can see the reference aRef is of type A.
I can see why you are confused by it. By the same procedure you have accessed the overridden methods for ex. the method identifyClass() but the not the variables which directly proves the first line that I have written .
Now in order to access the variable you can do ((Superclass)c).var
Note here that the Superclass can be many levels up for example
A<-B<-C. That is C extends B and B extends A. If you wanted the value of var of A then you could have done ((A)c).var .
EDIT: as one of the users have pointed out this 'trick' does not apply to static methods, because they are static.

Java inheritance overriding instance variable [duplicate]

This question already has answers here:
Java Inheritance - instance variables overriding
(3 answers)
Closed 10 years ago.
I am learning java. I have a doubt in inheritance. When a child class extends parent class and parent class has a method which refers to a instance variable declared in parent. But the child class dint override this method and has declared instance variable with same name as the parent. In this case instance variable from child will be referred or parent will be referred. Below is the code snippet
class Parent {
int a;
Parent() {
System.out.println("in Parent");
a = 10;
}
void method() {
System.out.println(a);
}
}
class Child extends Parent {
int a;
Child() {
System.out.println("in Child");
a = 11;
}
}
public class Test {
public static void main(String args[]) throws IOException {
Parent p1 = new Child();
p1.method();
}
}
The output I get is
in parent
in child
10
Can someone please make me understand why its referring parent class's instance variable a and not child class's a.
Another doubt is, I understood hiding the method, when there is a static method in parent and child class also has declared a static method with same signature. Here hiding means ? what method is getting hidden ? If its the parent method can you please explain me ?
Thanks in advance.
Java instance variables cannot be overridden in a subclass. Java inheritance doesn't work that way.
In your example, there is no method hiding (or overriding or overloading) going on.
There is hiding of instance variables though. In class child, the declaration of a hides the declaration of a in parent, and all references to a in the child class refer to the child.a not the parent.a.
To illustrate this more plainly, try running this:
public static void main(String args[]) throws IOException {
child c1 = new child();
parent p1 = c1;
System.out.println("p1.a is " + p1.a);
System.out.println("c1.a is " + c1.a);
System.out.println("p1 == c1 is " + (p1 == c1));
}
It should output:
p1.a is 10
c1.a is 11
p1 == c1 is true
This demonstrates that there is one object with two distinct fields called a ... and you can get hold of both of their values, if the access permits it.
Finally, you should learn to follow the standard Java identifier conventions. A class name should ALWAYS start with a capital letter.
Instance variables are not overriden in sub-class. If you define a variable in your class with the same name as in your super class it's called shadowing of variables inheritance and polymorphism doesn't apply for instance variables. if you define method() in parent and override it in Child class. the below would invoke the Child's method() due to run-time polymorphism printing 11
parent p1 = new child();
invokes Child constructor
with the super() call invoke's parent's constructor
Print's "in the parent" and initializes Parent's a to 10
print's in child and initializes Childs a to 11
p1.method();// this invokes Child's method() during run-time
As you are not overriding method() in child class, when the statement,
parent p1 = new child();
is executed, parent version of method() will be executed, and the only a value known to parent class is its own a. hence it will print a=10 (as it is on the stack at that time).
finally, you are just shadowing the variable a from parent class to child class.
When you do this
Parent P1=new Child();
what JVM do is
first Initialize Parent()
||
second Initialize Child()
So , first Parent constructor get called and then child's , but the output value will be 11 , because p1 is referring to child's object.
The issue is you created a child instance and stored it in a reference of the parent. Hence, when your access the object's property, JVM refers to the parent's variable value.
In case, it would have been a child class's reference variables, you would have received the child class' variable value.
The above is a feature of Java.
As you create the Instance of Parent. so in run time the compiler call the parent object ,
try the Below code.
public static void main(String args[]) throws IOException {
child c1 = new child();
c1.method();
}

Upcasting in Java and two separate object properties

Trying to understand upcasting in Java. Recently observed strange behavior.
Example:
public class A extends B {
public int i = 2;
public void printI() {
System.out.println("print i = " + this.i);
}
public static void main(String[] args) {
B a = new A(); // <- upcasting here
System.out.println("i = " + a.i);
a.printI();
}
}
class B {
public int i = 1;
public void printI() {}
}
//Output:
//i = 1
//print i = 2
Seems, that upcasted object has two separate "i" properties. One "i" accessible directly (a.i) and the other through methods of child class (a.printI()).
Looks like upcasted object gets properties from superclass and methods from child class.
How object can have two separate "i"s?!
Seems, that upcasted object has two separate "i" properties.
Firstly, it's worth being clear about terminology. There's no such thing as an "upcasted object" - and "i" is a field in each of A and B.
But yes, there are two separate fields here. It's not like one field "overrides" another or anything like that.
It's not clear what you were trying to achieve, but the declaration of i in A shadows the declaration of i in B. See section 6.4 of the Java Language Specification for more information.
Note that in almost all cases, fields should be private - at which point the hiding really doesn't matter, as you wouldn't try to refer to a variable which wasn't declared in the class you're coding in anyway.
That's how Java works. You have both fields "available", they just happen to have the same name. When you reference from the subclass, it is hiding the superclass' version, but it is still there.

java inheritance resolution in case of instance methods and variables

As per java, instance method resolution is based on runtime types of the arguments.
But while resolving instance variable it uses different approach as shown below.
Output of program is ..
Child
Parent
ParentNonStatic
Here First output is based on runtime types of the argument but third output is not.
can any one explain about this ?
public class Child extends Parent {
public static String foo = "Child";
public String hoo = "ChildNonStatic";
private Child() {
super(1);
}
#Override
String please() {
return "Child";
}
public static void main(String[] args) {
Parent p = new Parent();
Child c = new Child();
//Resolving method
System.out.println(((Parent) c).please());
//Resolving Static Variable
System.out.println(((Parent) c).foo);
//Resolving Instance Variable
System.out.println(((Parent) c).hoo);
}
}
class Parent {
public static String foo = "Parent";
public String hoo = "ParentNonStatic";
public Parent(int a) {
}
public Parent() {
}
String please() {
return "Tree";
}
}
When you upcast an object, you don't actually change the content of the item you are referring to, just the way you treat it. So when you upcast c to parent and call please(), you invoke please() on a Parent, but the dynamic type is still a child, so what really gets invoked is the overridden version in child and "Child" is printed.
When you upcast c and refer to foo, you are not making a function call. The compiler can determine at compile time what you are referring to. In this case, the field foo of an object that has the type parent. Fields and static fields are not overridden. Instead, they are hidden. What you actually end up doing with this upcast is helping Java pick the hidden version (the one from Parent) rather than the one from child. Similarly, with hoo you get the version of the parent.
Here's some info from the JAva tutorial: http://java.sun.com/docs/books/tutorial/java/IandI/hidevariables.html
In Java you don't actually "cast". Java doesn't actually do anything when you ask for a cast except to check if the object can be cast to that type thus why casts can throw a "ClassCastException".
The compiler though understands casts and thus uses them to validate that method calls are appropriate.
With respect to static fields, the compiler in fact removes any instance variables and references the field via the class according whatever the return type is.
Parent p = new Parent();
Child c = new Child();
Parent pc = new Child();
System.out.println(c.foo); // will print Child
System.out.println(p.foo); // will print Parent
System.out.println(pc.foo); // will print Parent
System.out.println(((Child)pc).foo) // will print Child
Fields seem to work the same way.
I think in a nutshell that java does dynamic binding on methods and static binding on fields.
Fields aren't overridden in the same way methods are. By casting c to Parent, you're indicating that ".hoo" refers to the "hoo" field on Parent, not the one on Child. (To put it in a more technically correct way, field access isn't polymorphic.)

Categories

Resources