Executed Python code:
class Test(object):
item = 0
def __init__(self):
print(self.item)
def test(self):
print(self.item)
class Subclass(Test):
item = 1
s = Subclass()
s.test()
gives:
1
1
Executed analogical Java code:
public class Test {
int item = 0;
Test(){
System.out.println(this.item);
}
void test(){
System.out.println(this.item);
}
public static void main(String[] args){
Subclass s = new Subclass();
s.test();
}
}
class Subclass extends Test {
int item = 1;
}
gives:
0
0
Apparently, Java method inherited from base class (Test) uses also base class’ member variables. Python method uses the member variable of derived class (Subclass).
The question: Is there any way to achieve the same or at least similar behaviour in Java like in Python?
Objects in Python are pretty much just like Dictionaries in Python. You can think of each instance of
TestandSubclassas a Dictionary that is updated by the__init__code and assignments in the body of the class you declare. You can picture the code you wrote working something like this:Python uses duck-typing, so
itemis just some property of whatever you happen to have an instance of. Notice that you don’t ever actually have to declare item—you just assign a value. This is why you’re able to “override” the value in the sub-class—because you’re actually just overwriting the old value of the same field. So in the example you gave, theiteminSubclassisn’t actually overriding theiteminTest; rather, they are the same field in a Python object instance.In Java fields actually belong to specific classes. Notice how in your code you actually have two declarations of the field
int item: one inTestand one inSubclass. When you re-declare theint iteminSubclassyou are actually shadowing the original field. See Java in a Nutshell: 3.4.5. Shadowing Superclass Fields for more info.I’m not sure exactly what you’re trying to do with your example, but this is a more idiomatic Java approach:
Notice how the value of
itemis set via a constructor argument rather than by simple assignment. Also notice howitemisprivateand that there is now a getter and setter method to access it. This is more Java-style encapsulation.That seems like a lot of code, but a good IDE (such as Eclipse or IntelliJ) will auto-generate a lot of it for you. I still think it’s a lot of boiler-plate though, which is why I prefer Scala—but that’s a whole different discussion.
Edit:
My post grew so long that I lost track of why I wanted to introduce getters and setters. The point is that by encapsulating access to the field you’re able to do something more like what you had in Python:
Now the
itemfield has effectively been overridden since all access to it is done through the getter and setter, and those have been overridden to point at the new field. However, this still results in0 1in the output rather than the1 1you were expecting.This odd behavior stems from the fact that you’re printing from within the constructor—meaning the object hasn’t actually been fully initialized yet. This is especially dangerous if a
thisreference is passed outside the constructor during construction because it can result in outside code accessing an incomplete object.