Last Updated on January 31, 2023 by Prepbytes
Object-oriented Programming (OOPs) is a programming paradigm based on the concept of objects The characteristics of an object define it and serve as its data. And the functions of the object are its methods, which define what the object is capable of. Because it allows our code to correspond with actual things in the real world, object-oriented programming is particularly well-liked.
What is OOPs in Python Language
Object Oriented Programming System referred to as OOPS in programming is to create a program using classes and objects is a paradigm for or methodology for programming. Every entity is treated as an object by OOPS.
Python’s object-oriented programming is focused on objects. Any OOPS-based programming that is developed solves our problem but takes the shape of objects. For a particular class, we are free to produce as many objects as we choose.
A logical object might be any file on your computer. Files can retain data, be downloaded, shared, and have other behaviors. They have properties like file name, file location, and file size
Difference between Procedural Oriented & Object Oriented Programming
|Object Oriented Programming (OOP)||Procedural Oriented Programming (POP)|
grams divided into objects | Programs divided into functions |
| Object Oriented Programming(OOP) is a bottom-up approach | Procedural Oriented Programming (POP)
is a top-down approach |
| It is more secure and supports inheritance | It is less secure and not supports inheritance |
| Use of access modifiers ‘public’, ‘private’,’ protected. | No use of access modifiers |
Main OOPs Concepts
- Data Abstraction
A class is a collection of objects. It is a logical entity or blueprint of the object which contains some attributes and methods. if you have a Car class, then it should contain an attribute and method, i.e. type, color, model year, company name, etc. Because every car
has its own type either it can be an SUV, hatchback, or sedan .similarly in color and company name. But there are many things which are similar in all cars like four tires.
So make a class of car that acts as a template where all the similar feature is present for a particular class. In simple words, we can say that class defines the structure of how things look or a basic overview.
The class can be defined by using the class keyword followed by name of the class.
. . .
class CAR: pass
Explanation: In the above example class is a keyword and the class name is CAR and ‘pass’ is commonly used as a placeholder.
It is an instance of the class or we can say an entity that has behavior and state with it. It is the real implementation of a class that has collections of data(variables) and methods (functions) that access the data.
An object consists of State, Behaviour, and Identity.
State: We can represent the state with help of attributes of an object and it also reflects the properties of an object.
Behavior: we can express the behavior with help of methods of an object and also reflects the response of an object to other objects.
Identity: It enables one object to interact with another object and gives a unique name to an object
Explanation: It is referred to as instantiating an object when a new instance of a class is created. The class name and parentheses can be used to create an object. The object of a class can be assigned to any variable.
The constructors in Java and C++ are comparable to the init method. As soon as a class object is created, it is executed. Any initialization you want to perform on your object can be done with the method.
Here we build some objects utilizing the self and init methods after defining a class.
# your code goes here class Car: #class attribute tyre = 4 def __init__(self, name, year, cartype): self.name = name self.year = year self.cartype = cartype c1 = Car("city", 2015, "sedan") c2 = Car("bolt", 2019, "hatch-back") print(c1.tyre,end=" ") print(c1.name,end=" ") print(c2.year,end=" ") print(c2.cartype)
4 city 2019 hatch-back
Explanation: In the above python program we create a class with the name Car and here tyre is a class attribute which is 4. In general, we observe that cars have 4 tires. So here class act as a template in which every object of class Car has 4 tyres. so we did not have to mention it again and again for every object. This makes our work easier that’s why we do object-oriented programming .it reduces redundancy and more simple to understand the code. here we have made two objects of class Car c1 and c2 which have attributes (name, year,cartype) .this are object attributes.
Note: Warning: If we do not pass the required arguments while creating the object, it will throw a TypeError: TypeError: init() missing 3 required positional arguments: ‘name’, ‘year’ and ‘cartype’.
Here we create class and objects with methods
class Car: #class attribute tyre = 4 def __init__(self, name, year, cartype): self.name = name self.year = year self.cartype = cartype def power (self): if (self.year>2015): return("car power is HIGH") else: return ("car power is LOW") c1 = Car("city", 2015, "sedan") c2 = Car("bolt", 2019, "hatch-back") print(c1.power()) print(c2.power())
car power is LOW car power is HIGH
Explanation: In the above we have taken the same class Car and here we create a function in class
With the name power. This function is called a method, here power function can directly call by the object by the ‘.’ operator. In this code, we make a condition that if the year of an object means a year of a car is greater than 2015 then the car power is high and if the car year is less than 2015, then the car power is low. so when we make c1 and c2 objects with proper argument .so by this argument or parameters, the power function runs the condition that either car power is low and either car power is high. we can say that we use the power function for both objects c1 and c2 by using the class template. here power is a method of the class.
Two terms, "poly" and "morphs," make up the term polymorphism. Poly denotes numerous, whereas morph refers to the shape. We comprehend polymorphism as the ability to complete a task in various ways.
Polymorphism refers to the existence of various forms. Polymorphism in programming refers to the usage of the same function name (but various signatures) for several kinds. The data types and quantity of arguments utilized in the function are the main differences
# your code goes here #program to understand polymorphism # len() here this len() function use for string length print(len("Prepbytes")) # len() and here it use for list print(len([1, 2, 3]))
Explanation: In the above python program we use len() function for calculating the length of string (“Prepbytes”) which of 9 length and we also use len() function for calculating the length of list
Which is of 3 lengths So this polymorphism.
Encapsulation is a technique for preventing direct user access to some object components, preventing users from seeing the state values for all of an object’s variables. Data members and data functions or methods connected to an instantiated class or object can both be concealed using encapsulation.
Users won’t be aware of how classes are built or stored, which is data hiding. Users will only be aware of the passing and initialization of values.
More adaptability gives you the option to make variables read or write-only. It includes data hiding and provides more flexibility
Example : class as it encapsulates all the data like member function and variables
A key component of the object-oriented paradigm is inheritance. Because we can use an existing class to construct a new class rather than creating it from start, inheritance allows the program to reuse code.
With inheritance, the child class gains access to all the data members, functions, and properties declared in the parent class. A child class may additionally offer its particular implementation of the parent class’s functions. We will go into great detail about inheritance in this lesson segment.
A derived class in Python can inherit from its base class by simply putting the base class’s name in brackets after the derived class name. To inherit a base class into a derived class, consider the following syntax.
Type of Inheritance in Python
It enables a derived class to inherit characteristics from a single-parent class.
It enables a derived class to inherit properties from an immediate parent class which in turn inherits properties from his parent class.
It inheritance enables more than one derived class to inherit properties from a parent class.
It enables one derived class to inherit properties from more than one base class.
A combination of many inheritance types is called hybrid inheritance. According to multiple inheritances, the class is descended from the two classes. The base class, however, is not one of the parent classes. This class is derived.
Data abstraction is the process or technique which provides only essential information and hides its core or background implementation. In general, abstraction is the process of stripping the details from the user In order to accomplish this, data abstraction provides a condensed version of the underlying data while hiding its complexity.
OOP (Object Oriented Programming) is a programming paradigm in Python that focuses on organizing code into objects. An object is a self-contained entity with attributes (data) and methods (functions). The main principles of OOP are inheritance, encapsulation, and polymorphism.
Inheritance allows objects to inherit attributes and methods from parent objects, making it easier to reuse code. Encapsulation is the practice of keeping the implementation details of an object hidden from the rest of the program. Polymorphism allows objects to respond to the same method call in different ways, depending on their specific implementation.
Classes are used to define objects in Python, and instances of classes are created by calling the class a function. The init method is used to initialize an object’s attributes. Access to object attributes can be controlled through the use of properties, methods, and decorators.
OOP is particularly useful for organizing complex programs, as it allows for the encapsulation of related data and behavior in a single object. This makes it easier to reason about the code, and makes it easier to maintain and extend the code over time.