Sitemap
SHARU HAASAN .T

SHARU HAASAN .T

OBJECT ORIENTED CONCPETS IN PYTHON

--

INTRODUCTION:

Object-Oriented Programming or known as OOPs is a programming paradigm that consists of principles like abstraction, encapsulation, inheritance, modularity, and polymorphism. Using the concept of OOPs is a highly efficient way of writing a program.

ADVANTAGES OF OOPS:

Re-usability of the program, Data Redundancy, Easy maintenance of the code, Security.

CONCEPTS 0F OOPS:

  1. Class

2. Objects

3. Abstraction

4. Encapsulation

5. Inheritance

6. Polymorphism

CLASS:-

A straight forward answer to this question is- A class is a collection of objects. Unlike the primitive data structures, classes are data structures that the user defines. They make the code more manageable.

SYNTAX :

class class_name :   
class_content

EXAMPLE:

Consider the case of a car showroom. You want to store the details of each car. Let’s start by defining a class first-

class Car: pass

OBJECT:-

An Object is an instance of a Class. Python is object-oriented programming language that stresses on objects i.e. it mainly emphasizes functions. Objects are basically an encapsulation of data variables and methods acting on that data into a single entity.

  • State
  • Behavior
  • Identity

SYNTAX:

obj = object_name()

EXAMPLE:

obj1 = pen()print(obj1)

ABSTRACTION:-

Data Abstraction refers to displaying only the essential information without any of the background details.

Get SHARU HAASAN .T’s stories in your inbox

Join Medium for free to get updates from this writer.

In simple words, it is used to make things less complicated for a user. A class with at least one abstract function is known as an abstract class.Any class with at least one abstract function is an abstract class. In order to create an abstraction class first, you need to import ABC class from abc module. This lets you create abstract methods inside it. ABC stands for Abstract Base Class.

EXAMPLE:

Press enter or click to view image in full size

ENCAPSULATION:-

Data Encapsulation refers to the wrapping up of similar data into a single unit. This also means, hiding valuable information to prevent unauthorized user access. Encapsulation is also an essential aspect of object-oriented programming.

It is used to restrict access to methods and variables. In encapsulation, code and data are wrapped together within a single unit from being modified by accident.

EXAMPLE:

Now for actually preventing the access of attributes/methods from outside the scope of a class, you can use “ private members “. In order to declare the attributes/method as private members, use double underscore ( __ ) in the prefix. Such as- self.__name or def __method(); Both of these lines tell that the attribute and method are private and access is not possible from outside the class.

Press enter or click to view image in full size

INHERITANCE:-

The process by which a child class (Subclass) inherits the data from its parent class (Superclass) is known as inheritance. By using inheritance, we can create a class which uses all the properties and behavior of another class.

The new class is known as a derived class or child class, and the one whose properties are acquired is known as a base class or parent class. It provides the re-usability of the code.

There are 5 types of Inheritance:

  • Single InHeritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

EXAMPLE:

Press enter or click to view image in full size

POLYMORPHISM:-

This is a Greek word. If we break the term Polymorphism, we get “poly”-many and “morph”-forms. So Polymorphism means having many forms. In OOP it refers to the functions having the same names but carrying different functionalities.

Polymorphism is one of the most important concepts applied in Object-Oriented Programming. The ability of an object of a class to behave in a different way when approached with a message or an action is known as polymorphism.

EXAMPLE:

Press enter or click to view image in full size

When the function is called using the object audi then the function of class Audi is called and when it is called using the object bmw then the function of class BMW is called.

--

--