Object Oriented Programing (OOP) | Python
Previous article :
Introduction
Object-oriented programming or OOP is an object-oriented programming method.
The existing program is a combination of several small pre-existing components.
This can make the programmer’s job easier in developing programs.
Objects that are interrelated and arranged in a group are called classes.
In time, these objects will complement each other to solve complex program problems.
Previously, developers had to focus on the logic to be manipulated, with OOP developers could focus more on the objects to be manipulated.
The main purpose of oop is to solve the problem of program complexity, so that the program is more structured.
There are various basic concepts used in OOP-based programming models, including:
- Class
- Object
- Constructor
- Abstract
- Inheritance
- Encapsulation
- Polymorphism
Class
Class is a collection of objects, Property, methods. Example :
To create a class in python using the keyword “class” followed by the name of the class and the sign “:”. and can be continued with class content
Object
Now we can use the class named MyClass to create objects :
Constructor
Constructor in python is a __init__() function.
To understand the meaning of classes we have to understand the built-in __init__() function.
All classes have a function called __init__(), which is always executed when the class is being initiated.
Use the __init__() function to assign values to object properties, or other operations that are necessary to do when the object is being created:
Example :
Create a class named Person, use the __init__() function to assign values for name and age :
Abstract
Abstract is a concept where in a class there are properties or methods that are abstract or have no clear value
To abstract the in python method, use the keyword “@abstractmethod”. example :
Inheritance
Inheritance is the concept of inheritance. where a property or method is inherited in a class under the parent class. example :
Encapsulation
Encapsulation is the restriction of access to properties or methods
Using OOP in Python, we can restrict access to methods and variables. This prevents data from direct modification which is called encapsulation. In Python, we denote private attributes using underscore as the prefix i.e single _ or double __.
Polymorphism
Polymorphism is an inheritance of a method or property with the same name but different properties for each derived method. example :