What is Object-Oriented Programming?
It’s a programming concept of “objects” that can contain data and code. The data is contained in the form of fields, and the code is in the form of procedures. The feature of objects is that procedures are attached to them and can access/modify the object’s data fields.
Structure of OOP:
Classes: A class is a blueprint for creating objects. It defines a set of attributes (data) and methods (functions) that the objects of that class can have.
In this example, the Car class defines three attributes (make, model, and year) and two methods (start_engine and stop_engine).
Objects: An object is an instance of a class. It has its own set of data and can perform its own set of methods.
In this example, my_car is an object (instance) of the Car class that has its own make, model, and year data, and can perform the start_engine method.
Inheritance: Inheritance allows one class to inherit properties and methods from another class.
In this example, we define the ElectricCar class as a subclass of Car using the syntax class ElectricCar(Car):. This means that ElectricCar inherits all the properties of Car, including its __init__ method, which we override in ElectricCar to add a battery_size attribute. To call the overridden method of the base class, we use the super() function to call the __init__ method of the base class with the same arguments as those passed to ElectricCar.
Now we can create objects of both classes and call their respective methods:
As you can see, the ElectricCar object inherits the start_engine method from the Car class and can also call its own charge_battery method. This allows us to reuse the code from the Car class and add more specific functionality to the ElectricCar class, without having to redefine everything from scratch.
Polymorphism: Polymorphism allows objects of different classes to be treated as if they were of the same class.
In this example, the start_vehicle function takes any object that has a start_engine method (which both Car and ElectricCar have) and calls that method on it, demonstrating polymorphism.
Encapsulation: Encapsulation is the practice of keeping data and methods that operate on that data within the same object. This can help prevent outside code from modifying the data directly.
In this example, the Square and Circle classes both define their own area() methods to calculate their respective areas. The get_shape_area() function takes a shape object as an argument and calls its area() method to calculate its area, without caring whether the shape is a Square or Circle object. This is an example of polymorphism, where objects of different classes can be used interchangeably as long as they have a common interface or parent class (in this case, the area() method).
As you can see, even though my_square and my_circle are instances of different classes, we can still pass them to the get_shape_area() function and get their areas calculated in a polymorphic way. This makes the code more flexible and easier to maintain, as we can add more shapes to our program and calculate their areas without having to modify the get_shape_area() function.
Overall, OOP is powerful and widely used in industry to develop complex software systems. By providing a way to organize code into modular, reusable objects, OOP helps to make code more flexible, maintainable, and readable.