Inheritance
derived class inherits properties from base class
derived class
based class
Polymorphism
implementing same method in different context.
method with same name implementing different way
+
For Instance
Institution:
Faculty Details
Students Details
Non Faculty Details
Other Details
Personal Details : : Name , DOB, Address
Faculty Details : Designation, salary, Attendence
Students Details : Progress, Attendence
Non Faculty Details : Designation, salary, Attendence
class Point:
def init (self,a=0,b=0):
self.x=a
self.y=b
def str (self):
return "(%d,%d)"%(self.x, self.y)
def add (self, p2):
p3=Point()
p3.x=self.x+p2.x
p3.y=self.y+p2.y
return p3
p1=Point(10,20)
p2=Point()
print("P1 is:",p1)
print("P2 is:",p2)
p4=p1+p2
print("Sum is:",p4)
Key points to remember
Create a Class : class MyClass:
Create Object : p1 = MyClass()
The __init__() :
the built-in __init__() function every class
which is always executed when the class is being initiated
We use to assign values to object properties
Object Methods : p1.myfunc()
The self Parameter :
reference to the current instance of the class
Modify Object Properties : p1.age = 40
Delete Object Properties : del p1.age
delete properties on objects by using the del keyword
Delete Objects : del p1
delete objects by using the del keyword
The pass Statement :
Class cannot be empty class Person:
For some reason have a class definition with no content pass
Self Parameter
class Person:
def __init__(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John", 36)
p1.myfunc()
Class
data
functions
student
roll_no
name
write
read
syntax:
object.name=class_name()
Class class Student:
Student_RollNo=10
Student_Name=“John”
data
functions
student
roll_no
name
write
read
functions
syntax:
object.name=class_name()
class Dog:
# A simple class
# attribute
attr1 = "mammal"
attr2 = "dog"
# A sample method
def fun(self):
print("I'm a", self.attr1)
print("I'm a", self.attr2)
# Driver code
# Object instantiation
Rodger = Dog()
# Accessing class attributes
# and method through objects
print(Rodger.attr1)
Rodger.fun()