Sitemap

Object Oriented Programming

1 min readSep 24, 2019

--

The basic idea behind an object-oriented programming is to combine both data and associated procedures into a single unit which operate on the data.Such a unit is called an object.

An object has two characteristics::

1: Attributes

2: Behavior

Class:

A class is a blueprint for the object.

Example: class cat:
pass

Object:

Object is simple a collection of data and methods that act on those data.An object is an instantiation of a class.

Example: obj = cat()

here,obj is object of class cat.

Methods:

Methods are function defined inside the body of a class.

example: def__init__(self ,name,age):
self.name = name
self.age = age
#instance method def sing(self,song):
return "{}sing{}".format(self.name,song)
def dance(self):
return"{}is now dancing".format(self.name)
#instance the object
Blu = Cat("Blu",10)
print(blu.sing("Happy"))
print(blu.dance())

Constructors:

Get Priti Pandey’s stories in your inbox

Join Medium for free to get updates from this writer.

Class function that begins with double underscore(_)are called special function as they have special meaning.

class ComplexNumber:
def__init__(self,r = 0,i = 0)
self.real = r
self.imag = i
def getData(self):
print("{0}+{1}j".format(self.real,self.imag))
c1 = ComplexNumber(2,3)
c1.getData()
c2= ComplexNumber(5)
c2.attr = 10
print((c2.real,c2.imag,c2.attr))
c1.attr

Encapsulation:

1: Hiding the private details of a class from other objects.

2: We can restrict access methods and variables.

3: This prevent data from direct modification which is called encapsulation.

Inheritance:

1: A process of using details from a new class without modifying existing class.

2: The newly formed class is a derived class.

3: The existing class is a base class.

Polymorphism:

1:A concept of using common operation in different ways for different data input.

2: Polymorphism is an ability to use common interface for multiple from(data types.)

--

--