Object-Oriented Programming with Python: A Practical Example

Python Blogs

Object-Oriented Programming with Python: A Practical Example

Hey there! If you’re diving into the world of programming, you might have come across the term Object-Oriented Programming (OOP). It’s a fundamental concept in software development that helps in creating modular and reusable code. In this post, we’ll explore OOP using Python with a practical example—a vehicle system! We’ll demonstrate core concepts like classes, inheritance, encapsulation, and object creation.

Defining the Core Classes

Let’s begin by defining two main classes: Engine and Vehicle. The Engine class represents the engine of a vehicle, while the Vehicle class can represent any type of vehicle, like a car, bus, or truck.

simple class definition in python

Here, the Engine class has an __init__ method to initialize the engine's horsepower (HP). The start() method prints a message when the engine starts, displaying the horsepower.

Inheriting from the Vehicle Class

To enhance our example, we can create a subclass called Car. This allows us to add more specific attributes and behaviors for cars while still inheriting common features from the Vehicle class.

simple class inheritance in python

In this Car class, we:

· Inherit from the Vehicle class using super().

· Define additional attributes like model, color, and engine (which uses the Engine class).

· Encapsulate the color attribute, making it private by prefixing it with double underscores (__color).

· Add a display_details() method to print out the car's details and start the engine.

Creating Objects and Using the Classes

Now, let’s create objects (instances) from our classes to see them in action.

object creation from a parent class in python

Output:

output of a object method created from parent class in python

We created a sample_vehicle object of the Vehicle class, which starts a vehicle from Tata.

Next, let’s create a car using the Car class:

object creation from a child class in python

Output:

output of a object method created from child class in python


Here, the my_car object inherits the start() method from Vehicle, and we also use the display_details() method from the Car class to showcase specific details about the car.

Key Concepts in OOP

· Encapsulation: We hid the color of the car by making the __color attribute private, which protects the data from being modified directly.

· Inheritance: The Car class inherited properties and methods from the Vehicle class, allowing code reuse and logical structuring.

· Object Creation: Objects like sample_vehicle and my_car were created from classes, each with its own attributes and behaviors.

Conclusion

This example illustrates how you can use OOP in Python to model real-world entities like vehicles. By structuring your code with classes, inheritance, and encapsulation, you can build more flexible and maintainable programs. OOP not only enhances your programming skills but also prepares you for larger software development projects. OOP is not only used in Core Python, but it is being used in Python Frameworks for web development like django, flask and Advanced Automation, Machine Learning, Artificial Intelligence, IoT and more. So, keep experimenting with these concepts, and soon you'll be creating complex systems with ease!