Java programmers making a move to Python often struggle with Python’s approach to object-oriented programming (OOP). The approach to working with objects, variable types, and other language capabilities taken by Python vs Java are quite different. It can make switching between both languages very confusing.
This article compares and contrasts object-oriented programming support in Python vs Java. By the end, you’ll be able to apply your knowledge of object-oriented programming to Python, understand how to reinterpret your understanding of Java objects to Python, and use objects in a Pythonic way.
Over the course of this article, you’ll:
- Build a basic class in both Java and Python
- Explore how object attributes work in Python vs Java
- Compare and contrast Java methods and Python functions
- Discover inheritance and polymorphism mechanisms in both languages
- Investigate reflection across Python vs Java
- Apply everything in a complete class implementation in both languages
This article isn’t a primer on object-oriented programming. Rather, it compares object-oriented features and principles of Python vs Java. Readers should have good knowledge of Java, and also be familiar with coding Python. If you are unfamiliar with object-oriented programming, then check out Intro to Object-Oriented Programming (OOP) in Python. All Python examples will work with Python 3.6 or later.
Download Sample Code: Click here to download the free source code for the Java and Python objects in this article.
Object-Oriented Python Live Course: Want to design classes the Pythonic way? In Real Python’s Object-Oriented Python live course, you build a complete project with an instructor, from Python’s data model to choosing between inheritance and composition. Join the waitlist to hear about the next dates.
Sample Classes in Python vs Java
To begin, you’ll implement the same small class in both Python and Java to illustrate the differences between them. You’ll make modifications to them as the article progresses.
First, assume you have the following Car class definition in Java:
1public class Car { 2 private String color; 3 private String model; 4 private int year; 5 6 public Car(String color, String model, int year) { 7 this.color = color; 8 this.model = model; 9 this.year = year; 10 } 11 12 public String getColor() { 13 return color; 14 } 15 16 public String getModel() { 17 return model; 18 } 19 20 public int getYear() { 21 return year; 22 } 23} Java classes are defined in files with the same name as the class. So, you have to save this class in a file named Car.java. Only one public top-level class can be defined in each file.
A similar small Car class is written in Python as follows:
1class Car: 2 def __init__(self, color, model, year): 3 self.color = color 4 self.model = model 5 self.year = year In Python you can declare a class anywhere, in any file, at any time. Save this class in the file car.py.
Using these classes as your base, you can explore the basic components of classes and objects.
Object Attributes
All object-oriented languages have some way to store data about the object. In Java and Python, data is stored in attributes, which are variables associated with specific objects.
One of the most significant differences between Python vs Java is how they define and manage class and object attributes. Some of these differences come from constraints imposed by the languages, while others come from best practices.
Declaration and Initialization
In Java, you declare attributes in the class body, outside of any methods, with a definite type. You must define class attributes before they are used:
1public class Car { 2 private String color; 3 private String model; 4 private int year; In Python, you both declare and define attributes inside the class __init__(), which is the equivalent of Java’s constructor:
1def __init__(self, color, model, year): 2 self.color = color 3 self.model = model 4 self.year = year By prefixing the variable names with self, you tell Python these are attributes. Each instance of the class will have a copy. All variables in Python are loosely typed, and these attributes are no exception.
Read the full article at https://realpython.com/oop-in-python-vs-java/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]