Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overview of differences between inheritance in python and java

I have a background in java, and I'm learning python. I'll need to be using inheritance soon, and I want to find out what any key differences are between how things are done in each of them. I've had a look online and found some information about differences between the two and what to look out for, and I've found that python supports multiple inheritance, but I'd like to know about any other differences specific to inheritance. I don't need to be told syntax (unless there is something I really need to watch out for), I've already taken a look at it and I'll be fine with that.

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

I can't really find exactly what I'm looking for which is an overview of differences and what to watch out for.

like image 507
Bianca Avatar asked Sep 10 '11 12:09

Bianca


1 Answers

Java has a fairly straightforward inheritance model: classes must have one (and only one) parent . You can't inherit from multiple parents, although you can implement multiple interfaces, which can be seen as Java's 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 have noted, you can inherit from multiple (or no) parents. Note that with multiple inheritance you can get the "diamond problem." You should be aware of how Python resolves that and the implications this has when you refer to a parent class in Python (ie: who's your daddy?)

In Python, everything is dynamically bound, and since you can add members to an instance, not all instances of the same class are guaranteed to have the same members.

Finally, there's a slight difference in how constructors are overridden: In Java, children classes must call the parent's constructor (refinement overriding), whereas in Python children classes can override the constructor and not call the parent's constructor (replacement overriding).

like image 136
NullUserException Avatar answered Oct 06 '22 00:10

NullUserException