Overview of the differences between inheritance in python and java

I have a background in java and I am learning python. I will need to use inheritance in the near future, and I want to find out what are the key differences between how everything is done in each of them. I looked online and found information about the differences between them and what to look for, and I found that python supports multiple inheritance, but I would like to know about any other differences specific to inheritance. I don’t need to specify the syntax (if there isn’t something that I really need to follow), I already looked at it, and everything will be fine with this.

Python is not Java
Python Classes: Multiple Inheritance
Python for Java programmers

I cannot find exactly what I am looking for, which is an overview of the differences and what should be observed.

+4
source share
2 answers

Java has a fairly simple inheritance model: classes must have one (and only one) parent. You cannot inherit from multiple parents, although you can implement several interfaces that can be thought of as the Java version of "multiple inheritance."

Most methods in Java classes are dynamically (late) bound, with the exception of methods declared static , private and final in the parent class.

In Python, as you noted, you can inherit from multiple (or not) parents. Note that with multiple inheritance, you may get a problem. "You need to know how Python solves this and the consequences it has when you refer to a parent class in Python (i.e. Who is your dad?)

In Python, everything is dynamically connected, and since you can add elements to an instance, not all instances of the same class guarantee the same members.

Finally, there is a slight difference in how constructors are redefined: in Java, child classes must call the parent constructor (override overrides), while in Python child classes they can override the constructor and not call the parent constructor (replace replacement).

+4
source

The most important concept to understand based on java background is that class inheritance is a tool that is rarely needed to solve problems in python. The instant word for this idea is duck.

Most python programs access and invoke attributes on an object or another. As in java, you can get useful behavior by providing a function that expects an object of one type to be an object of another, more subtle type. For this to work in java, it is necessary that the replaced object either inherit from the expected type or implement the expected interface.

In python, this is not necessary at all; If the replaced object has all the attributes that you pass to it, then it will work.

The only reason for inheriting in python is that the superclass really, really does pretty much everything you need for the new type, and you only need to add a few extra actions.

The most common example of this is the iterator protocol in python. Any object that has the __iter__() method that returns the object itself, and the next() method that returns anything at all, is an iterator and a for statement may appear. str , list , dict , file and many other types that have nothing in common, and only the global object as a regular superclass, each implements an iterator protocol.

+2
source

All Articles