Difference between inheritance in Java and Python

Python code executed:

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 similar 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, the Java method inherited from the base class (Test) also uses member variables of the base class. The Python method uses a member variable of a derived class (subclass).

Question: is there a way to achieve the same or at least similar behavior in Java as in Python?

+5
source share
3 answers

Objects in Python are pretty much like Python dictionaries. You can treat each instance Testand Subclassas a dictionary, which is updated code __init__and assignments in the body of the declared class. You can imagine the code you wrote as follows:

class Test(object):         
    item = 0                # self['item'] = 0

    def __init__(self):
        print(self.item)    # print(self['item'])

    def test(self):
        print(self.item)    # print(self['item'])

class Subclass(Test):       
    item = 1                # self['item'] = 1

s = Subclass()              # Test.__init__({})
s.test()                    

Python duck-typing, item - , . , - . "" , . , , , item in Subclass item Test; , Python.

Java . , int item: Test Subclass. int item Subclass, . . Java : 3.4.5. .

, , Java:

public class Test {

    private int item;

    public Test() {
        this(0); // Default to 0
    }

    public Test(int item) {
        setItem(item);
        test();
    }

    public void test() {
        System.out.println(getItem());
    }

    public static void main(String[] args) {
        Subclass s = new Subclass();
        s.test();
    }

    public void setItem(int item) {
        this.item = item;
    }    

    public int getItem() {
        return item;
    }

}

class Subclass extends Test {

  public Subclass() {
      super(1); // Default to 1
  }

}

, item , . , item - private, getter setter . Java.

, IDE (, Eclipse IntelliJ) . , , Scala, .

Edit:

, , . , , , - , Python:

public class Test {
   // Same as above . . .
}

class Subclass extends Test {

  private int subclassItem = 1;

  public int getItem() {
    return subclassItem;
  }

  public void setItem(int item) {
    this.subclassItem = item;
  }

}

item , getter setter, , . 0 1 , 1 1, .

, , . , this , , .

+4

, item Test 0:

public class Test {
    int item = 0;

    Test(){
        System.out.println(this.item);
    }

    Test(int item) {
        this.item = item;
        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 {

    public Subclass() {
        super(1);
    }
}
+4

Use an initializer instead of reusing fields:

public class Test {
   int item = 0;

   ...
}

public class Subclass extends Test {
    {
        item = 1;
    }
}

Note. Depending on the structure of your package, you may declare itemhow protected.

+3
source

All Articles