Object Oriented Programming in Python

Object Oriented Programming in Python

Python is a wonderful programming language that allows the use of both functional and object-oriented programming classes. Objected-oriented programming as a subject has gained universal importance among programmers. The whole paradigm is based on the concept that everything is an object. These objects contain data and methods and can interact with each other.

Let’s see how our python coding tutors talk about the core of object-oriented programming and learn how it is beneficial for us to use this paradigm.

What is Object Oriented Programming in Python?

OOP languages are class-based, which indicates that a class defines the data characteristics and functions that are used to create objects, which are instances of the class. It means that we create classes that contain both data and functions. When you want to create something in memory, you create an object, which is an instance of that class.

Therefore, it means that a class is a collection of variables and methods that define a particular object type. Classes represent broad groups like Cars that have attributes. The class would state what attribute an instance would have like color, model, etc. but not the value of a specific object.

Our python coding tutors state that class templates are used as blueprints to create an Object. Each object has specific values to the attributes/properties defined in the class.

The Four Pillars of Object Oriented Programming

The four major pillars of any object-oriented programming are:

  • Inheritance: The child class takes over data and behavior from the parent class
  • Encapsulation: Encloses information about an object
  • Abstraction: Limiting exposure by using public methods
  • Polymorphism: Many methods do a similar task.

Inheritance

We often hear from relatives and friends – “you look exactly like your father” or “you behave so much like your mother” the reason behind the said statement is inheritance. Similarly, our python coding tutors explain that in OOPs inheritance means inheriting/passing on characteristics of the parent class to the child class. The class from which a class is derived is called the parent/base class and the new class that is derived is the child class.

By parent class extending attributes and methods to the child class, Inheritance supports the reusability of code. The child class not just extends the functionality of the parent class but can also add additional attributes and methods.

An Example often provided by coding tutors is: a library system has two new categories: Novel and Academics. Now, irrespective of whether the book is a novel or academic it would have similar attributes like title and author and common methods such as price() and discount(). Rewriting the codes for both of these in two classes is a waste of time and effort. Therefore, we can create a Book class (which shall have common attributes and methods) and inherit two child classes Novel and Academic. By inheriting we are reducing the effort of writing the same methods twice and if needed we can add additional functionality depending on the specific need of the class.

Encapsulation:

Object-oriented programming languages as stated by any python coding tutors should contain information inside an object and only selected information should be exposed to the outside world.

When an object is created from the class, the data and methods are encapsulated in that object. Encapsulation hides the code      implementation inside a class.

 

 

Encapsulation requires defining some fields as private and some as public.

  • Private/ Internal interface: Methods and properties, accessible from other methods of the same class.
  • Public / External Interface: Methods and properties, accessible also from outside the class.

Let’s take a Car for example. The Car using blinkers to turn is shared with the outside world whereas the engine is hidden.

Encapsulation helps in adding a layer of security where the developer decides what data can be seen through public methods. This allows us to make sure that important data information is not changed by external developers.

Abstraction

Abstraction of OOPs is like an extension of encapsulation. It uses simplified, high-level tools to deal with complex objects. It isn’t supported directly in Python. An abstract method is declared in the parent class, and a child class that inherits this class must have an implementation of this method. It helps in maintaining a similar structure throughout the subclasses. For example, we don’t need to know all details of how the engine of the car works to drive the car. Thus, abstraction hides the complex code and enables easier software maintenance. Another real-life example is ATM, we all perform actions like withdrawals, deposits, statements, etc. but have no idea of how the machine is working.

Polymorphism

Polymorphism is considered another very important concept of Object-Oriented Programming language by a python coding tutor. While using inheritance the problem comes when the child class has its way of implementing a method. Polymorphism allows the same method to execute different behaviors in two ways: Method Overriding and Method Overloading.

 

Method Overriding

It states that a child class can provide a different implementation of a method present in the parent class. The result is based on whether the object of the parent or the child class is calling the function.

Method Overloading

Methods or functions having the same name, but a different number of parameters passed into the method call. Different results occur depending on the number, type, and sequence of parameters passed in.

It might be difficult for someone who is used to the top-down approach of coding to understand the OOPs concepts. But once we understand what, how, and why of this approach any developer would adapt to this way of coding. It not only makes coding simpler but debugging errors or adding functionalities in this approach is much easier.  Are you seeking introductory or advanced online coding classes for kids? At SkoolOfCode, we offer project-based learning modules where students use live code to find logical and inventive solutions to problems. So, why wait and Book a FREE trial class today?

 

By: Ms. Divya Dalal, an educator working with SkoolofCode taking Scratch and Python classes. She is an MCA with, a Master’s in Technology in Software Engineering.