Day 8 : constructor and inheritance
Hello guys,
There are lots of stuff to do in this quarantine, so i started my YouTube channel check here ๐
If you like do subscribe❤.
Previously we learn about basic concepts of object oriented programming and about class and objects in python.
Constructor
Constructor is special function which is used to initialize the members of class.
In other languages like C++ or java constructor is defined as same name as that of classname
But
Constructors can be of two types.
How to create constructor in python? ๐ค
In python, method __init__ is used in class. This method is called when the class is instantiated. We can pass any number of arguments at the time of creating the class object, depending upon __init__ definition. It is mostly used to initialize the class attributes. Every class must have a constructor, even if it simply relies on the default constructor.
Code:
class Employee:
def __init__(self,name,id):
self.id = id;
self.name = name;
def display (self):
print("ID: %d \nName: %s"%(self.id,self.name))
emp1 = Employee("Captain America",101)
emp2 = Employee("Ironman",102)
#accessing display() method to print employee 1 information
emp1.display();
#accessing display() method to print employee 2 information
emp2.display();
Output;
Non parameterized
Code:
class Student:
# Constructor - non parameterized
def __init__(self):
print("This is non parametrized constructor")
def show(self,name):
print("Hello",name)
student = Student()
student.show("John")
Output :
Parameterized constructor
Code :
class Student:
# Constructor - parameterized
def __init__(self, name):
print("This is parametrized constructor")
self.name = name
def show(self):
print("Hello",self.name)
student = Student("John")
student.show()
Output:
Now we can move on to inheritance.
As inheritance is basic and if you don't understand this it will be difficult to understand other stuffs.
So I recommend you to watch
check this by telusko.
As he teachs things very easily so watch those videos of inheritance
If you get any difficulties then ask at our group.
- Parameterized Constructor
- Non-parameterized Constructor
How to create constructor in python? ๐ค
In python, method __init__ is used in class. This method is called when the class is instantiated. We can pass any number of arguments at the time of creating the class object, depending upon __init__ definition. It is mostly used to initialize the class attributes. Every class must have a constructor, even if it simply relies on the default constructor.
Code:
class Employee:
def __init__(self,name,id):
self.id = id;
self.name = name;
def display (self):
print("ID: %d \nName: %s"%(self.id,self.name))
emp1 = Employee("Captain America",101)
emp2 = Employee("Ironman",102)
#accessing display() method to print employee 1 information
emp1.display();
#accessing display() method to print employee 2 information
emp2.display();
Output;
ID: 101 Name: Captain America ID: 102 Name: Ironman
Non parameterized
Code:
class Student:
# Constructor - non parameterized
def __init__(self):
print("This is non parametrized constructor")
def show(self,name):
print("Hello",name)
student = Student()
student.show("John")
Output :
This is non parametrized constructor Hello John
Parameterized constructor
Code :
class Student:
# Constructor - parameterized
def __init__(self, name):
print("This is parametrized constructor")
self.name = name
def show(self):
print("Hello",self.name)
student = Student("John")
student.show()
Output:
This is parametrized constructor Hello JohnJjKk
Now we can move on to inheritance.
As inheritance is basic and if you don't understand this it will be difficult to understand other stuffs.
So I recommend you to watch
check this by telusko.
As he teachs things very easily so watch those videos of inheritance
If you get any difficulties then ask at our group.
Contact here if you get any problem
❤❤Quarantine python group link ❤❤
8805271377 WhatsApp
Follow here ❤
@mr._mephisto_ Instagram
There will be no restrictions just feel free to learn.
Share and take one more step to share knowledge to others.
Believe in yourself ๐ค you are awesome.
Be safe, Be happy๐
Take care of yourself and your family
Of course watch movies and series๐ค๐
And follow the guidelines of government
Comments
Post a Comment