bython in Constructor Destructor Inheritence in hindi by unitdiploma
•Constructor
•Destructor
•Inheritence
Constructor
•Constructors are generally used for instantiating an object.
•The task of constructors is to initialize(assign values) to the data members of the class when an object of class is created.
•In Python the __init__() method is called the constructor and is always called when an object is created.
Syntax of constructor declaration :
def __init__(self): # body of the constructor
Types of Construtor
default constructor
•The default constructor is simple constructor which doesn’t accept any arguments.
•It’s definition has only one argument which is a reference to the instance being constructed.
parameterized constructor
•constructor with parameters is known as parameterized constructor.
•The parameterized constructor take its first argument as a reference to the instance being constructed known as self and the rest of the arguments are provided by the programmer.
The___init___() function
•To understand the meaning of classes we have to understand the built-in __init__() function.
•All classes have a function called __init__(), which is always executed when the class is being initiated.
•Use the __init__() function to assign values to object properties, or other operations that are necessary to do when the object is being created:
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person(“John”, 36)
print(p1.name)
print(p1.age)
The __init__() function is called automatically every time the class is being used to create a new object.
Destructors
•Destructors are called when an object gets destroyed.
•In Python, destructors are not needed as much needed in C++ because Python has a garbage collector that handles memory management automatically.
•The __del__() method is a known as a destructor method in Python.
•It is called when all references to the object have been deleted i.e when an object is garbage collected.
Syntax of destructor declaration :
•def __del__(self): # body of destructor
Note : A reference to objects is also deleted when the object goes out of reference or when the program ends.
Data Abstraction
•Data abstraction and encapsulation both are often used as synonyms.
•Both are nearly synonym 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.
Inheritance
•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
Create a Parent Class
Any class can be a parent class, so the syntax is the same as creating any other class:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
#Use the Person class to create an object, and then execute the printname method:
x = Person(“John”, “Doe”)
x.printname()
Create a Child Class
To create a class that inherits the functionality from another class, send the parent class as a parameter when creating the child class:
class Student(Person):
pass
Use the Student class to create an object, and then execute the printname method:
x = Student(“Mike”, “Olsen”)
x.printname()
Single Inheritance
class Animal:
def speak(self):
print(“Animal Speaking”)
#child class Dog inherits the base class Animal
class Dog(Animal):
def bark(self):
print(“dog barking”)
d = Dog()
d.bark()
d.speak()
Multiple Inheritance
class Animal:
def speak(self):
print(“Animal Speaking”)
#The child class Dog inherits the base class Animal
class Dog(Animal):
def bark(self):
print(“dog barking”)
#The child class Dogchild inherits another child class Dog
class DogChild(Dog):
def eat(self):
print(“Eating bread…”)
d = DogChild()
d.bark()
d.speak()
d.eat()
Multiple Inheritance Contd..
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print(d.Summation(10,20))
print(d.Multiplication(10,20))
print(d.Divide(10,20))
The issubclass(sub, sup) method
The issubclass(sub, sup) method is used to check the relationships between the
specified classes. It returns true if the first class is the subclass of the second class, and false otherwise.
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print(issubclass(Derived,Calculation2))
print(issubclass(Calculation1,Calculation2)
The isinstance (obj, class) method
The isinstance() method is used to check the relationship between the objects and classes. It returns true if the first parameter, i.e., obj is the instance of the second parameter, i.e., class.
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print(isinstance(d,Derived)