Python is an object-oriented programming language. This means that the program design is based around objects, rather than functions and logic.
Each object is created using a blueprint known as a class . Each class has attributes to contain the data an object needs to work with.
Each class also contains functions, called methods that perform operations on objects.
An object is an instance of a class. So, you could have a class called “car” and use it to define an object called “merc.”
You’ll need the source files in the directory Chapter08.
Principles of OOP
The four principles of OOP are encapsulation, inheritance, polymorphism, and abstraction.
Encapsulation
With encapsulation , you restrict access to methods and attributes within a certain class. This prevents accidental modification of data and unwanted changes to other objects.
Inheritance
A class can inherit all the methods and attributes from another class. If a class called “person” had the attributes name, age, and dob, we could use this class to define two other child classes called “student” and “staff.” Both inherit the methods and attributes from the “person” class.
Polymorphism
Polymorphism allows us to define methods in the child class with the same name as defined in the parent class. This is known as method overriding.
Polymorphism also allows us to define methods that can take many forms.
Abstraction
Abstraction is the process of reducing objects to their essence so that only the necessary elements are represented. In other words, you remove all irrelevant information about an object in order to reduce its complexity.
Classes and Objects
The self keyword represents the current instance of the class (i.e., the object created from the class). By using the self keyword, you can access the attributes of the object itself.
Let’s take a look at a program. Open the file class.py. Here, we’ve defined our “Person” class.
To create an object from the class, call the class Person(...) and pass any data using parenthesis ( ). Assign the new object to a variable, for example, person.
So in our example, to use the attributes we use the dot notation, with the object name followed by the attribute name as we can see here:
Similarly, if we want to use the methods of an object, we use the dot notation, with the object name followed by the method name as we can see here:
Class Inheritance
We mentioned earlier that inheritance means a class can inherit all the methods and attributes from another class.
The child classes inherit all the attributes and methods of the parent classes. Child classes can include any additional attributes and methods that are not accessible from other classes.
Open the file inherit.py. Here, we’ve created a class called person . We’ve also created two child classes called student and staff.
We can create a lecturer object from the class Staff.
To reference our attributes, we use the dot notation .
Polymorphic Classes
Open the file polyclass.py. Here, we’ve defined a class for triangle, square, and circle. Notice each class has a method called getArea().
This means we can call the .getArea() method for each object created.
Method Overriding
With method overriding , you can define a method with the same name in the child class as in the parent class. The method in the child class overrides the method in its parent class.****
Have a look at methodoverride.py. Here, we’ve defined the classes discussed earlier. The methods in the child classes have the same name as in the parent class. Each method is redefined and specific to the class. Let’s see what happens when we create our objects and call the methods.
Give it a try .
Lab Exercises
- 1.
Declare a new class called Vehicle without any attributes and methods.
- 2.
Add some attributes to the Vehicle class such as
Name
Speed
Mileage
- 3.
Add a method to the Vehicle class to return the vehicle name.
- 4.
Create a child class called Car that will inherit all the variables and methods of the Vehicle class.
- 5.
Create a child class called Taxi.
- 6.
Add a method to the Taxi class to collect the fair.
Summary
Python is an object-oriented programming language.
A class is a blueprint for an object.
Each class has attributes to contain the data an object needs to work with.
Each class contains functions, called methods that perform operations on objects.
Encapsulation restrict access to methods and attributes within a certain class.
A class can inherit all the methods and attributes from another class. Child classes inherit methods and attributes from the parent.
Polymorphism allows us to define methods in the child class with the same name as defined in the parent class.
Abstraction is the process of reducing objects to their essence so that only the necessary elements are represented.
You can define a class using the class keyword.
To use methods defined in an object, use the dot notation.
Method overriding allows you to define a method with the same name in the child class as in the parent class.