
Play Store Application link – Java to Python in 17 Steps – App on Google Play
Github project link – https://github.com/kuldeep101990/Python_step6
Object-Oriented Programming (OOP) is a paradigm many Java developers are familiar with. Python’s OOP concepts align closely with Java’s, but Python simplifies syntax and introduces unique features like dynamic typing and duck typing.
Classes and Objects in Python vs. Java
In Java, everything revolves around classes and objects. Similarly, Python uses classes to define blueprints for objects.
Java Example:
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
void introduce() {
System.out.println("Hi, my name is " + name);
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person("Alice", 30);
p.introduce();
}
}
Python Equivalent:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"Hi, my name is {self.name}")
p = Person("Alice", 30)
p.introduce()
self
vs. this
- In Java,
this
is used to reference the current instance. - In Python,
self
serves the same purpose but must be explicitly declared as the first parameter of instance methods.
Example:
class Example:
def show(self):
print("This is self in Python")
Constructors (__init__
)
In Java, constructors initialize objects. Python achieves this with the __init__
method.
Java Example:
Person(String name, int age) {
this.name = name;
this.age = age;
}
Python Equivalent:
def __init__(self, name, age):
self.name = name
self.age = age
Inheritance and Method Overriding
Inheritance in Python is straightforward and supports method overriding like Java.
Java Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
Python Equivalent:
class Animal:
def sound(self):
print("Animal makes a sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
Polymorphism
Polymorphism in Python is simpler because it leverages dynamic typing.
Python Example:
class Bird:
def sound(self):
print("Bird chirps")
class Cat:
def sound(self):
print("Cat meows")
# Polymorphism in action
for animal in [Bird(), Cat()]:
animal.sound()
Encapsulation
Python provides encapsulation using underscores _
for private attributes. Unlike Java’s access modifiers, Python relies on conventions.
Python Example:
class BankAccount:
def __init__(self, balance):
self._balance = balance # Private attribute
def get_balance(self):
return self._balance
Duck Typing and Dynamic Binding
Python’s flexibility allows objects to be used based on their behavior rather than type.
Python Example:
def play_with_pet(pet):
pet.sound()
class Dog:
def sound(self):
print("Dog barks")
class Cat:
def sound(self):
print("Cat meows")
play_with_pet(Dog())
play_with_pet(Cat())
__str__
vs. toString()
Python’s __str__
is equivalent to Java’s toString()
for string representation of objects.
Java Example:
@Override
public String toString() {
return "Person[name=" + name + ", age=" + age + "]";
}
Python Equivalent:
def __str__(self):
return f"Person[name={self.name}, age={self.age}]"
Complete Python Program
Here’s a Python program demonstrating OOP concepts:
class Animal:
def __init__(self, name):
self.name = name
def sound(self):
print("Animal makes a sound")
class Dog(Animal):
def sound(self):
print(f"{self.name} barks")
class Cat(Animal):
def sound(self):
print(f"{self.name} meows")
class BankAccount:
def __init__(self, balance):
self._balance = balance
def get_balance(self):
return self._balance
# Demonstration
if __name__ == "__main__":
dog = Dog("Rex")
cat = Cat("Whiskers")
dog.sound()
cat.sound()
account = BankAccount(1000)
print(f"Account Balance: {account.get_balance()}")
How to Run:
- Save the code in a file named
oop_demo.py
. - Run it using the terminal:
python oop_demo.py
Sample Output:
Rex barks
Whiskers meows
Account Balance: 1000
This post gives you a practical understanding of Python’s OOP concepts. Experiment with these examples to strengthen your grasp!