Professional Documents
Culture Documents
Chapter 5
Chapter 5
Using Object:
To call the attribute or methods of class, object name with dot operator is used.
E. g.
class myclass:
"This is my second class" a = 10
def func_2(self):
print('Hello')
obj=myclass()
print(obj. doc )
print(obj.a)
obj.func_2()
self:
Class methods must have an extra first parameter in method definition.
We do not give a value for this parameter when we call the method, Python provides it.
If we have a method which takes no arguments, then we still have to have one argument –
the
self.
This is similar to „this‟ pointer in C++ and „this‟ reference in Java.
When we call a method of this object as
myobject.method(arg1, arg2),
this is automatically converted by Python into
MyClass.method(myobject, arg1, arg2)
Method Overloading:
Like other languages (for example method overloading in C++) do,
python does not supports method overloading.
We may overload the methods but can only use the latest defined method.
e.g
class student:
def disp(self,roll,name):
print(roll,name)
def disp(self):
print("WELCOME")
obj=student()
#obj.disp(23,"Ram")
obj.disp()
Method Overloading:
class overlod:
def fun(self,a,b=None):
print(a)
def fun(self,a,b=None):
print(a,b) obj=overlod() obj.fun(89) obj.fun(23,45)
#O/P:
89 None
23 45
Inheritance:
Inheritance is an important aspect of the object-oriented paradigm. Inheritance provides
code
reusability to the program because we can use an existing class to create a new class
instead of
creating it from scratch.
In inheritance, the child class acquires the properties and
can access all the data members and functions defined in the parent class.
A child class can also provide its specific implementation to the functions of the parent
class.
Inheritance:
In python, a derived class can inherit base class by just mentioning the base in the bracket after
the derived class name.
Syntax:
class derived-class(base-class):
<class-suite>
A class can inherit multiple classes by mentioning all of them inside the bracket. Consider the
following syntax.
Syntax:
class derived-class(base-class1, base-class2…,base-classn):
<class-suite> >
Inheritance allows us to define a class that inherits all the methods and properties from
another class.
Parent class is the class being inherited from, also called base class.
Child class is the class that inherits from another class, also called derived class.
Types of Inheritance
Single Level
Multilevel
Multiple
Single Inheritance:
Single inheritance enables a derived class to inherit properties from a single parent class, thus
enabling code reusability and the addition of new features to existing code
class Animal:
def speak(self):
print("Animal Speaking")
class Dog(Animal):
def bark(self):
print("dog barking")
d = Dog() d.bark() d.speak()
O/P:
dog barking
Animal Speaking
Multilevel Inheritance:
Deriving a new class from old derived class.
This is similar to a relationship representing a child and a grandfather
Syntax:
class A:
#Body of class class B(A):
#Body of class class C(B):
#Body of class
Multiple Inheritance:
When a class is derived from more than one base class it is called multiple Inheritance.
The derived class inherits all the features of the base case
Method Overriding:
When a method in a subclass has the same name, same parameters or signature
and same return type(or sub-type) as a method in its super-class, then the method in the subclass
is said to override the method in the super-class.
Example Method Overriding:
Method Overriding:
class Bank:
def getroi(self):
return 10;
class SBI(Bank):
def getroi(self):
return 7;
class ICICI(Bank):
def getroi(self):
return 8;
b1 = Bank()
b2 = SBI()
b3 = ICICI()
print("Bank Rate of interest:",b1.getroi()); print("SBI Rate of interest:",b2.getroi());
print("ICICI Rate of interest:",b3.getroi());
#O/P:
Bank Rate of interest: 10
SBI Rate of interest: 7
ICICI Rate of interest: 8
Composition in Python:
In composition one of the classes is composed of one or more instance of other classes.
In other words one class is container and other class is content and if you delete the container
object then all of its contents objects are also deleted.
0 E.g.
class Salary:
def_init_(self, pay):
S1=Salary(100)
self.pay = pay
def get_total(self):
return (self.pay*12)
ans=S1.get_total()
Print(“Total=”ans)
Composition in Python: (Two Classes) {DO it from PPT}
Composition in Python:(Three Classes)
Composition in Python:(Three Classes)
Encapsulation in Python:
Encapsulation is one of the fundamental concepts in object-oriented programming
(OOP).
It describes the idea of wrapping data and the methods that work on data within one unit.
This puts restrictions on accessing variables and methods directly and can prevent the
accidental modification of data.
To prevent accidental change, an object‟s variable can only be changed by an object‟s
method.
Those type of variables are known as private varibale.
A class is an example of encapsulation as it encapsulates all the data that is member
functions,
variables, etc.
Protected members:
Protected members (in C++ and JAVA) are those members of the class which cannot be
accessed outside the class but can be accessed from within the class and it‟s subclasses. To
accomplish this in Python, just follow the convention by prefixing the name of the member by a
single underscore “_”.
Private Members:
Private members are similar to protected members, the difference is that the class members
declared private should neither be accessed outside the class nor by any base class. In Python,
there is no existence of Private instance variables that cannot be accessed except inside a class.
However, to define a private member prefix the member name with double underscore “ ”.