OOPS Concept
OOPS Concept
o Class
o Object
o Method
o Inheritance
o Polymorphism
o Data Abstraction
o Encapsulation
Class
The class can be defined as a collection of objects.
It is a logical entity that has some specific attributes and methods.
For example: if you have an employee class, then it should contain an
attribute and method, i.e. an email id, name, age, salary, etc.
Syntax
1. class ClassName:
2. <statement-1>
3. .
4. .
5. <statement-N>
Object
The object is an entity that has state and behavior.
It may be any real-world object like the mouse, keyboard, chair, table,
pen, etc.
Everything in Python is an object, and almost everything has attributes
and methods.
All functions have a built-in attribute __doc__, which returns the
docstring defined in the function source code.
When we define a class, it needs to create an object to allocate the
memory.
Example:
1. class car:
2. def __init__(self,modelname, year):
3. self.modelname = modelname
4. self.year = year
5. def display(self):
6. print(self.modelname,self.year)
7.
8. c1 = car("Toyota", 2016)
9. c1.display()
Output:
Toyota 2016
In the above example, we have created the class named car, and it has
two attributes modelname and year.
We have created a c1 object to access the class attribute.
The c1 object will allocate memory for these values.
We will learn more about class and object in the next tutorial.
Method
The method is a function that is associated with an object.
In Python, a method is not unique to class instances. Any object type
can have methods.
Inheritance
Inheritance is the most important aspect of object-oriented
programming, which simulates the real-world concept of inheritance.
It specifies that the child object acquires all the properties and
behaviors of the parent object.
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.
Polymorphism
Polymorphism contains two words "poly" and "morphs".
Poly means many, and morph means shape.
By polymorphism, we understand that one task can be performed in
different ways.
For example - you have a class animal, and all animals speak. But they
speak differently. Here, the "speak" behavior is polymorphic in a sense
and depends on the animal.
So, the abstract "animal" concept does not actually "speak", but
specific animals (like dogs and cats) have a concrete implementation
of the action "speak".
Encapsulation
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.
Data Abstraction
Data abstraction and encapsulation both are often used as synonyms.
Both are nearly synonyms because data abstraction is achieved
through encapsulation.
Abstraction is used to hide internal details and show only
functionalities.
Abstracting something means to give names to things so that the
name captures the core of what a function or a whole program does.
3. It simulates the real world entity. So It doesn't simulate the real world. It
real-world problems can be easily works on step by step instructions
solved through oops. divided into small parts called
functions.