Stay Classy Part 2: Java vs Python
Published: 2019-11-16 01:39:58

---
title: Stay Classy Part 2: Java vs Python
published: true
description: 
tags: 
---

Classes are an interesting part of object oriented programming, and for some languages they make perfect sense. Take Java for example, a classic object oriented language, and one that makes excellent use of classes. Learning about classes in Java is very important because it is very difficult, nigh impossible, to do do anything without classes in Java. In Java every program, every script, and of course every object, is a class. Every Java file must be defined as a class in order to function properly. Programmers used to working in more relaxed languages like JavaScript or Python might chafe at having to worry about the class hierarchy and things like typing or inheritance (more on that later). But for Java it makes sense and keeps the program from trying to fit the proverbial square peg in the round hole. It depends more on the programmer to specify the type of peg to use the hole, but if we have assured the program that it is a round peg, it can safely operate as a round peg. Python has less distinction between types, and only deals with typing when it attempts to perform an operation on an object and complains if the object is not what it expected. 

Java and Python can both define classes for objects, and although Python can write classes that resemble Java classes, Java is much stricter about its typing than Python. To start with, let’s look at how a class is defined in Java vs how it is defined in Python. Here’s how you would define a class in Java:

```Java
public class MyClass{
	
	public static void main(String[] args) {

	}

}
```

You might notice that the first function is called ‘main’. This is because any Java class that needs to be executable must have a function called main, which contains the first code that will be run when the program is run. Additionally the parameter ‘args’ contains any arguments passed to the program upon execution. Class files that do not need to be run and only exist to define classes for other programs do not need to have a ‘main’ function. This is already more complex and requires more explanation than a Python class file, but that is because the main body of a Java program must be contained in a class. For contrast, let’s have a quick look at a Python class:

```Python
class MyClass():
  
```

And that’s it. A class in any language is mostly just a wrapper for a collection of functionality and some data, but in a language like Python this is especially true. Java requires classes for it’s very framework, where in Python it’s just a convenience. So let’s look at where classes become even more important in Java, and why inheritance is useful. Here is a typical function definition in Java:

```Java
public int myFunction(int a, String b){

} 
```

How does typing figure into this definition? Notice the `int` after `public`? That tells the function that it will be retuning an object of type `int`, or an integer. This is important because when this function is called elsewhere the output can only be stored in an object defined as an integer. Now also notice the `int` and `String` before the parameter names. This means that even before we do anything with them in this function we already know that `a` and `b` are an integer and a string, respectively. This means we can treat them as such in the function. It also means that when calling the function it will throw errors if variables are passed that do not match the parameter types. This keeps things very orderly, but it can also feel a little restrictive. Abstraction is something that can help with that, as I discussed in my previous post about classes.