Sample Classes in Python vs Java
So, here we have a class
Car. We define three fields—the color, model, and its year. We have a constructor assigning values to each of those fields and some typical get methods to obtain the values of those fields.
So, comparing our Java class to our Python class, we define class
Car and then we have our
.__init__() method, which is similar to a constructor. Behind the scenes they’re not really the same, but if you want to view writing a Java constructor, it would go into this dunder method—a method that begins and ends with two underscores (
__) is referred to as a dunder method—called
Similarly, this statement defines the field
.model and this statement defines the field
.year. In the remainder of this course, we will dig deeper into these differences—why it’s so much smaller, why we’re missing things like
private—but the first thing we’re going to be taking a look at will be the declaration and initialization of fields.
Become a Member to join the conversation.