Python is a
object-oriented programming language. Any object-oriented programming language’s key concepts include
A class is a logical way to group fields that hold values
(data) and associated methods that operate on those fields into a single unit. The class provides the structure or blueprint for creating objects. It describes how an object looks and operates. It helps us to create a user-defined data type.
All classes must be defined by a class definition, which consists of two parts: class header and class body.
The class header declares the class names along with other information about the class such as the name of its superclass, whether it implements any interfaces, access specifier that determines the visibility. These modifiers declare whether the class is abstract or final. The class body is enclosed within braces and consists of fields, methods, constructors, and other declarations. At its most superficial level, the class takes the following form.
class ClassName: <statement-1> ............ <statement-N>
class Sum: x = input("Type First Number: ") y = input("Type Second number: ") sum = int(Sum.x) + int(Sum.y) print("The sum is: ", sum)
If a field is declared
static, then there is one field for the entire class instead of one per object. A class’s static field is often referred to as a class variable because the static field is associated with a class and not with individual instances of the class. A static field gets memory only once for the whole class, no matter how many class objects are created.
Methods are truly the heart and soul of the programs. A method is a self-contained block of code that performs a specific task. They provide a way of defining an object’s behavior(s), i.e., what the object does. Methods serve to break up large and complex calculations that might involve many code lines into more manageable chunks. All execution that takes place in any application is within a method.
Consider an example of an architect of a house who specifies a house design, i.e., how it will look like, but doesn’t build any house. On the basis of this design, a builder can build one or more houses. Similarly, a class specifies the format for creating objects. The mere definition of a class doesn’t create any object. It only describes how the objects will look like when they are created. So a class definition is just a template or a blueprint based on which one or more objects, which are real-world entities, can be created.
When defining a class, an object must be generated to assign the memory. Consider the scenario below.
class house: def __init__(self,rooms, area): self.rooms = rooms self.area = area def display(self): print(self.rooms,self.area) h1 = house("5", 2000) h1.display()
Polymorphism is a compelling concept that allows you to design amazingly flexible applications. The word ‘Polymorphism: is derived from two Greek words,
'poly,' which means many, and
'morphos,' which means forms. So, polymorphism implies the ability to take many forms.
Polymorphism can be defined as one interface multiple methods, which means that one interface can perform different but related activities. In programming terms, it means the ability by which a single variable of a given type can be used to reference objects of different types and automatically call the method specific to the type of object the variable references. It enables a single method call to behave differently depending on the type of object the call applies.
We have been dealing with objects, which are the building blocks of the object oriented programming language. A programmer may use multiple objects belonging to different classes in some situations, which may have some relationship among themselves as they share some standard features.
Let us consider an example of a python program containing objects of two different classes,
Motorcycle, for maintaining car and motorcycle information, respectively. Both Car and Motorcycle classes exhibit some relationship as both are vehicles and share some standard features like
specification, etc. To represent the relationship between classes, we use the concept of Inheritance.
Inheritance is one of the key concepts in the Python Object-Oriented Programming language that enables you to organize classes in a hierarchical form. Just like a child inherits his parents’ characteristics and adds certain new characteristics of his own, a programmer can write a new class that can inherit the accessible fields and methods of the existing class and may also add new fields and methods. The inclusion of members of the existing class in a new class so that they are accessible in the new class is known as class inheritance. The existing class that is being inherited is called the superclass, and the new class that inherits the functionality is called the subclass. In some other programming languages, a superclass can also be referred to as a
base class, or an
ancestor, and the subclass is referred to as a
derived class, or a
We may use the same name for many Python functions in function overloading, but with a different number or type of parameters.
The characteristics of an object may or may not be visible outside of the class definition. It will benefit if you named attributes with a
double underscore prefix, and those attributes are not accessible to outsiders.
class Total: __Sum = 0 def count(self): self.__Sum += 1 print self.__Sum counter = Total() counter.count() counter.count() print counter.__Sum
Operator overloading is the method of utilizing an operator according to the operand in various forms. You may adjust the way a Python operator operates on numerous data types.
class Bubble: def __init__(self, Volume, Area): self.Volume = Volume self.Area = Area def __str__(self): return 'Bubble (%d, %d)' % (self.Volume, self.Area) def __add__(self,other): return Bubble(self.Volume + other.Volume, self.Area + other.Area) b1 = Bubble(2,10) b2 = Bubble(5,-2) print (b1 + b2)
A subclass inherits methods from a superclass. However, in certain situations, the subclass needs to modify the implementation (code) of a method defined in the superclass without changing the parameter list. It is achieved by overriding or redefining the method in the subclass.
To override a subclass’s method, we redefine the subclass method with the same name, same return type, and the same parameter list as its superclass counterpart. When the method with the same name exists in both the superclass and in the subclass, the object of the subclass will invoke the method of the subclass, not the method inherited from the base class.
class Parent: # define parent class def M1(self): print ('Parent Method') class Child(Parent): # define child class def M1(self): print ('Child Method') child = Child() # instance of child child.M1() # child calls overridden method
A constructor is a special method called whenever an object is created,which is used to initialize the class members.
In Python, we have special built-in class methods that start with a
double underscore (__), and they have a significant meaning.
The name of the constructor will always be
Every class must have a constructor, and if you don’t explicitly create a constructor; it creates a default constructor by itself.