From Java to Python: Mastering Object-Oriented Programming in Python for Intermediate Developers

Muhammad Naufal Hanif
4 min readDec 26, 2023

--

Object-oriented programming (OOP) is a programming paradigm that uses objects as a foundation unit for developing programs. OOP have a lot of advantages, such as simplicity in handling and program development, along with increased code reusability.

Neither Java nor Python are programming languages that support OOP. However, there are noticeable differences between these two languages. Java is a programming language that is statically-typed, while Python is dynamically-typed. this means that data type in Java must be explicitly determined, whereas, in Python, the interpreter will automatically determine the data type from variables.

Source: Linkedin

Let’s dive into the OOP Concept in Python, shall we?

In Python, most of the OOP Concepts are similar to Java OOP, here are some OOP Concepts in Python:

  • Class: Blueprints to create an object.
  • Object: Instance of the class.
  • Attribute: Property of an object.
  • Method: Function that is associated with an object.
  • Inheritance: Allows one class to inherit attributes and methods from another class.
  • Polymorphism: Allows objects of different classes to respond to the same message in different ways.
  • Encapsulation: Wraps data and functions in a single unit.

OOP Differences in Java and Python:

OOP Differences

Examples of basic OOP Concepts:

Here is some example of the Person class that is used to keep information about the person, such as name and age:

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def say_hello(self):
print(f"Hello, my name is {self.name}. I am {self.age} years old.")


person1 = Person("John Doe", 30)
person2 = Person("Jane Doe", 25)

print(person1.name)
# John Doe

print(person2.age)
# 25

person1.say_hello()
# Hello, my name is John Doe. I am 30 years old.

person2.say_hello()
# Hello, my name is Jane Doe. I am 25 years old.

It’s quite similar to Java, right? This is even more concise than Java, don’t you think?

This is an example of not using the Class keyword in Python:

def Person(name, age):
def __init__(self, name, age):
self.name = name
self.age = age

def say_hello(self):
print("Hello, my name is {} and I am {} years old.".format(self.name, self.age))

person1 = type("Person", (), {
"name": name,
"age": age,
"say_hello": say_hello
})

person1.say_hello()

To create a Person class without the Class keyword, we use the type() function with two arguments:

1. The first argument is the class name: “Person”.

2. The second argument is a dictionary that defines the class attributes and methods:

  • name: The name attribute of type string.
  • age: The age attribute of integer type.
  • say_hello(): The say_hello() method that accepts one argument self.

Once the Person class is created, we can create objects from that class using the operator ().

Examples of advanced OOP concepts:

Here is an example of the Shape class (known as abstract class) used as the parent of the Rectangle and Circle classes:

import math


class Shape:
def __init__(self, name):
self.name = name

def get_area(self):
raise NotImplementedError

# Inheriting the shape class
class Rectangle(Shape):
def __init__(self, name, width, height):
super().__init__(name)
self.width = width
self.height = height

def get_area(self):
return self.width * self.height

# Inheriting the shape class
class Circle(Shape):
def __init__(self, name, radius):
super().__init__(name)
self.radius = radius

def get_area(self):
return math.pi * self.radius ** 2


rectangle = Rectangle("Rectangle", 10, 20)
circle = Circle("Circle", 10)

print(rectangle.get_area())
# 200

print(circle.get_area())
# 314.1592653589793

The Rectangle and Circle classes are subclasses of the Shape class. The Rectangle class has two attributes, namely width and height. The Circle class has one attribute, namely radius. Both of them inherit the shape class (parent class).

Examples of OOP App:

The following is an example of a guess number game.

import random


class Game:
def __init__(self):
self.number = random.randint(1, 100)
self.guesses = 0

def guess(self, guess):
self.guesses += 1
if guess == self.number:
print("You guessed correctly! It took you {} guesses.".format(self.guesses))
return True
elif guess < self.number:
print("Your guess is too low.")
return False
else:
print("Your guess is too high.")
return False


game = Game()
user_input = int(input("Guess a number between 1 and 100: "))
while not game.guess(user_input):
pass

This program uses the Game class to create a number guessing game. The Game class has two attributes, namely number and guesses. The class also has one method, namely guess().

The guess() method is used to process the player’s guess. This method will check whether the player’s guess is correct. If the player’s guess is correct, then this method will print a success message and return True. If the player’s guess is wrong, then this method will print an error message and return False.

In this program, the Game class is used to store the number that the player must guess, as well as the number of guesses that the player has made. The guess() method is used to process the player’s guess and print a success or error message.

Object-oriented programming (OOP) is a powerful and versatile programming paradigm. By mastering OOP, you can write programs that are more modular, reusable, and maintainable.

Here are some tips for mastering OOP in Python:

  • Start by learning basic OOP concepts, such as classes, objects, attributes, and methods.
  • Practice using the OOP examples given in this article.
  • Try creating your own program using OOP.

By following the tips above, you will be able to master OOP in Python quickly and easily.

Hopefully, this article is useful for you! If you have any questions or comments, feel free to leave a message below.

--

--

No responses yet