PREV NEXT
Basic Features of OOP(Object Oriented Programming)
1. Object:
- Object is nothing but collection of data members and method in single unit called object.
- Object is a physical entity.An object might be anything like person,vehicle,things etc.To Confirm an entity is an object we can cosider three components .
They are below,
i. state:
It indicates the characteristics of real world entity and also it refers to the present condition or state of the entity.Ex:color,height,weight,name etc.
ii. Behaviour:
It is nothing action/reflection of an object. Ex:Running,idle,walking etc.
iii. Identity:
It is an unique identification of the real world entity.It will differentiate to one to entity to another entity Ex:ID,Reg No,DL No Etc.
2. Class:
Class in nothing but collection of objects and also we can say class is blueprint where behaviour and attributes defined.Considered Animal is class and Dog, Monkey,Donkey, Cat etc all are the object for the class Animal.
Wild Animal
Data:
Name
Life_Time
kind
Methods:
walk()
sleep()
eat()
3. Encapsulation:
Wrapping of data and method,method should work on that data is called Encapsulation,i.e capsules
4. Abstraction:
Collection of essential data to our application and neglate the irrilavant data to our application
5. Polymorphism:
Single interface with multiple implememtation we can called polymorphism.We can achieve the poly morphism in two ways they are.
1. Function Oveloading
2. Operator Overloading
6. Inheritance:
Inheritance is the concept ,here we can create the new class by using alreay existing class.Newly created class we can call derive class or child class,already existing class we can call it as a base class or parent class.Python supports Simple,Multilevel and multiple
Access Specifiers:
There is no access specifiers in python like c,c++(public,private,protected),but we can achieve this mechanism in deifferent ways now we can see that
1. public:
- All data memebers and methods in python by default public access specifiers,Means we can acces data memebers and methods entire the source code
- For public access specifiers we can’t give any type special syntax it is normal variable and methods
ex:
1 2 3 4 5 6 7 |
class class_name: def method(): var1=value1 var2=value2 |
2. protected:
- protected data members and methods are accessable with in base class and derive class only.
- To identify this knid of access specifiers we can use the data member or method can start with single underscore “_” this we can call it as protected data members.But keep it in mind protected we can use only when we have subclasses in sorce code.
ex:
1 2 3 4 5 6 7 |
class class_name: def _method(): _var1=value1 _var2=value2 |
3. private:
- private data members and methods are not accesible anywhere except that class which is having data members and methods,but it can be inhereited the sub classes but can’t be accesasable.
- To identify this knid of access specifiers we can use the data member or method can start with double underscore “__” this we can call it as private access specifier.
ex:
1 2 3 4 5 6 7 |
class class_name: def __method(): __var1=value1 __var2=value2 |
self in python:
- self is not predefined keyword in python programming language.But mostly for understanding people are using the self keyword in python for object oriented programming language.
- When ever we are going to implement the class in python especially in methods we are using self keyword as an argument ,because of when called class method by using obect of that class then interpreter intenally passed object as an argument to the method,so most people in class method they can consider first arument as an object argument,we can give any type of name not only self.
__init__() method (constructor):
This is the method called automatically once the object has been created with out invoking any object and it will call only one time entire life time of the program.Mostly this method is used to intialize data members with some default data,If require to do some functionality is need to after object created immediately.
Class Variables:
The variables which are we declare after immediately the class created we can call it as class variables .Class variables are we can access entire the application,the data is sharable by multiple objects.
Ex:
1 2 3 |
class class_name: class_variable |
Object Variables:
The variables which are dedicated for individual objects that variable we can called as object variables.
Ex:
1 2 3 4 5 |
class class_name: def method(): object_variable |
Example1:
How to create plain class and methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class student: def get_details(self): self.name="john" self.rno=429 self.fee=50000.00 def put_details(self): print("Name is:",self.name) print("Rno is",self.rno) print("Fee is:",self.fee) s=student() s.get_details() s.put_details() |
Output:
1 2 3 4 5 |
Name is: john Rno is 429 Fee is: 50000.0 |
Example2:
Creating class with having class variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
class student: database="student" def get_details(self,aname,arno,afee): self.name=aname self.rno=arno self.fee=afee def put_details(self): print("Name is:",self.name) print("Rno is",self.rno) print("Fee is:",self.fee) print("Database is:",self.database) s1=student() s2=student() print("Obect:1st Created Object Details") s1.get_details("john",425,42000.0) s1.put_details() print("Obect:2nd Created Object Details") s2.get_details("abraham",450,47000.0) s2.put_details() #student.name="hello" student.database="New_Student" print("************After Student DataBase*****************") print("Obect:1st Object Details") s1.put_details() print("Obect:2nd Object Details") s2.put_details() |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
Obect:1st Created Object Details Name is: john Rno is 425 Fee is: 42000.0 Database is: student Obect:2nd Created Object Details Name is: abraham Rno is 450 Fee is: 47000.0 Database is: student ************After Student DataBase***************** Obect:1st Object Details Name is: john Rno is 425 Fee is: 42000.0 Database is: New_Student Obect:2nd Object Details Name is: abraham Rno is 450 Fee is: 47000.0 Database is: New_Student |
Explanation:
1. The above example i have created the with class variable and object method variables
2. I have created two objects for that class names are s1,s2
3. I have given the diferent details for both objects, i have given database is “student”,Now for both object having “student”
4. Now i have changed the database with “new_student”,after that i have display the details,there if we observe it has been reflected in both objects
Example3:
Constructor or __init__ Method:
- constructos are the special memmber function it will get invoke automatically at the time of creation of an object,it will intialize the data members of that object.In python we can create the constructors with the help of __new__ () method and __init__() method.
- Here __new__() will create the object and __init__() will initialize the object.
Destuctor or __del__ Method:
destructos are the special memmber function it will get invoke automatically at the time of creation of destroying the object,it will un-intialize the data members of that object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
class student: def __init__(self,aname,arno,afee): self.name=aname self.rno=arno self.fee=afee def put_details(self): print("Name is:",self.name) print("Rno is",self.rno) print("Fee is:",self.fee) def __del__(self): print("Object destroyed") obj_s1=student("john",420,50000) obj_s2=student("abraham",450,47000.0) print("Obect:1st Created Object Details") obj_s1.put_details() print("Obect:2nd Created Object Details") obj_s2.put_details() print("*****Both objects are going to destroyed*******") del obj_s1 del obj_s2 |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Obect:1st Created Object Details Name is: john Rno is 420 Fee is: 50000 Obect:2nd Created Object Details Name is: abraham Rno is 450 Fee is: 47000.0 *****Both objects are going to destroyed******* Object destroyed Object destroyed |
Example4:
Function Overloading:
Having the same function name with multiple implememtation we can call it as a function overloading,function overloading will happen number of arguments,type of arguments and order of arguments.Function oveloading is not happen with respect to return type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class polymor: def add(self,a,b): return a+b def add(self,a,b): return a+b def add(self,a,b): return a+b fun_over=polymor() int_sum=fun_over.add(3,4) float_sum=fun_over.add(3.5,4.3) str_sum=fun_over.add("3","4") print("Int_sum is:",int_sum) print("float_sum is:",float_sum) print("str_sum is:",str_sum) |
Output:
1 2 3 4 5 |
Int_sum is: 7 float_sum is: 7.8 str_sum is: 34 |
Example 5:
Operator Overloading:
- The Operator which are available in python working fine with predefined data types,but theya are not compatible with user defined data types,so to make working fine with them we are going to take the help of operator overloading
- Below is the example program we are going to overloading the ‘+’ operator .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class addOvrld: def __init__(self, a = 0, b = 0): self.a = a self.b = b def __str__(self): return "({0},{1})".format(self.a,self.b) def __add__(self,other): a = self.a + other.a b = self.b + other.b return addOvrld(a,b) obj1 = addOvrld(2,3) obj2 = addOvrld(2,2) print("Addition of Two Objects is:",obj1 + obj2) |
Output:
1 |
Addition of Two Objects is: (4,5) |
suppose consider we not going to use any operator overloading concept to add two objects then what will happen
step 1:
class addOvrld:
def __init__(self, a = 0, b = 0):
self.a = a
self.b = b
consider above i have created the class with constructor,then below i have created two objects for that class
obj1 = addOvrld(2,3)
obj2 = addOvrld(2,2)
and then add the two objects
print(“Addition of Two Objects is:”,obj1 + obj2)
then i can get below error
TypeError: unsupported operand type(s) for +: ‘addOvrld’ and ‘addOvrld’
so as per this experiment we have understand we can’t add two objects directly it will give unexpected behaviour
Why __str__() method:
If we observe above code we have used __str__() method ,because if we try to acceess the object data it will not give proper expected output.To get expected output we are going to use inbuit __str__method
obj1 = addOvrld(2,3)
suppose if we try to print the obj1 without using __str__ () method,then we will get below output
<__main__.addOvrld object at 0xb720508c>
suppose if we try to print the obj1 by using __str__ () method inprogram,then we will get below output
(2,3)
How operator overload can work internally:
Once we have perform the addition operation between two objects,then it will call __add__() method,but intenally it will invoke the __add__ method like obj1.add(obj2)
Few more default function for operator overloading
Operator / Operation | Internatal implementation |
Addition / obj1+obj2
|
obj1.__add__(obj2)
|
Substraction / obj1-obj2
|
obj1.__sub__(obj2)
|
Multiplication / obj1*obj2
|
obj1.__mul__(obj2)
|
Division / obj1/obj2
|
obj1.__div__(obj2)
|
Modulus / obj1%obj2
|
obj1.__mod__(obj2)
|
Less Than / obj1<obj2
|
obj1.__lt__(obj2)
|
Equal to / obj1=obj2
|
obj1.__eq__(obj2)
|
Not Equal to / obj1!=obj2
|
obj1.__ne__(obj2)
|
Greater Than / obj1>obj2
|
obj1.__gt__(obj2)
|
Less Than equal to / obj1<=obj2
|
obj1.__le__(obj2)
|
Greater Than equal to / obj1>=obj2
|
obj1.__ge__(obj2)
|
Bitwise AND / obj1&obj2
|
obj1.__and__(obj2)
|
Bitwise OR / obj1 | obj2 | obj1.__or__(obj2) |
Bitwise XOR / obj1^obj2 | obj1.__xor__(obj2) |
Bitwise NOT / ~obj1 | obj1.__invert__(obj2) |
Bitwise Right Shift / obj1<<obj2 | obj1.__rshift__(obj2) |
Bitwise Left Shift / obj1>>obj2 | obj1.__lshift__(obj2) |