--- slideOptions: theme: white --- # Chapter 10 - Introduction to Object oriented programming ###### tags: `Computer programming` --- ## Learning objectives 1. Describe the basic concepts in object oriented programming, i.e. Encapsulation, inheritance and polymorphism 2. Implement object oriented programming in Python ## Introduction In the beginning, most of the programming languages are procedural. Procedural programming simply contains a series of computation steps to be carried out. Any given procedure might be called at any point during a program’s execution, including by other procedures or itself. The programming languages that are procedural include Basic, C, C++, Pascal, Python, PHP, Javascript, etc. Later on, a new programming paradigm, Object-oriented programming (OOP) was invented. In OOP, we use the concept of classification in Biology to describe the world and write the programs. The OOP has the following objectives. 1. ***Modularity***: The codes are written in terms of modules, which is easier for development and debugging. ![](https://i.imgur.com/nls2tHh.jpg) 2. ***Reusability***: The written object code can be reused. ![](https://i.imgur.com/n3vGY5x.jpg) 3. ***Portability***: The written code can be run on every machine and OS (cross-platform). ![](https://i.imgur.com/rCjQAgH.jpg) The OOP has the following three main concepts. 1. ***Encapsulation*** (Information hidden): Some information is hidden and cannot be read by other objects directly. Other objects still can read the data through the methods provided. ![](https://i.imgur.com/SgZy1Wn.jpg) 2. ***Inheritance***: Some features of the object can be inherited (Ctrl-C → Ctrl-V) from the superclass. ![](https://i.imgur.com/WRpCvxM.jpg) 3. ***Polymorphism***: The methods can have different behaviours in different situations. ## Implement object-oriented concepts in Python Python is a versatile programming language that supports various programming styles, including object-oriented programming (OOP) through the use of objects and classes. An object is any entity that has attributes and behaviors. For example, a `Poultry` is an object. It has - attributes - name, age, color, etc. - behaviour - dancing, singing, etc. Similarly, a class is a blueprint for that object. ### Class and Object <iframe src="https://trinket.io/embed/python3/2fcfb8530e" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> In the above example, we created a class with the name `Poultry` with two attributes: `name` and `age`. Then, we create instances of the `Poultry` class. Here, `poultry1` and `poultry2` are references (value) to our new objects. We then accessed and assigned different values to the instance attributes using the objects name and the . notation. ### Inheritance Inheritance is a way of creating a new class for using details of an existing class without modifying it. The newly formed class is a derived class (or child class). Similarly, the existing class is a base class (or parent class). Below shows an example. <iframe src="https://trinket.io/embed/python3/a6beb3d2fe" width="100%" height="425" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> Here, `chicken1` (the object of derived class `Chicken`) can access members of the base class `Poultry`. It's because `Chicken` is inherited from `Poultry`. ### Encapsulation Encapsulation is one of the key features of object-oriented programming. Encapsulation refers to the bundling of attributes and methods inside a single class. It prevents outer classes from accessing and changing attributes and methods of a class. This also helps to achieve data hiding. In Python, we denote private attributes using underscore as the prefix i.e single `_` or double `__`. <iframe src="https://trinket.io/embed/python3/5b61be2366" width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> In the above program, we defined a Poultry class. We used the `__init__()` method to store the maximum selling price of Poultry. Here, notice the following code. `p.__maxprice = 1000` Here, we have tried to modify the value of `__maxprice` outside of the class. However, since `__maxprice` is a private variable, this modification is not seen on the output. As shown, to change the value, we have to use a setter function i.e `setMaxPrice()` which takes price as a parameter. ### Polymorphism <iframe src="https://trinket.io/embed/python3/f8d24a89ea" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> In the above example, we have created a superclass: `Poultry` and two subclasses: `Chicken` and `Duck`. Notice the use of the `tweet()` method. The main purpose of the `tweet()` method is to render the shape. However, the process of tweeting of a chicken is different from tweeting of a duck. Hence, the `tweet()` method behaves differently in different classes. Or, we can say `tweet()` is polymorphic.