OOPs (Object Oriented Programming System) ========================================== High-level programming languages: C, C++, Java, Python etc. ==> classified into three types: 1) Functional Programming Languages ==> C, C++, Python etc. 2) Object Based Programming Languages ==> Class and object Ex: VB Script 3) Object Oriented Programming Languages ==> C++, Java, Python In C: printf(), scanf(), putchar(), getchar(), fprintf(), fscanf(), malloc(), calloc() add(a,b){ printf(a+b); } Object Oriented Programming System (OOPs): ========================================== Principles Encapsulation Polymorphism Inheritance Abstraction Encapsulation: ============== En+capsul ==> Binding/wrapping up of attributes (data members/variables) and methods (behaviors) into one unit as class is called as "Encapsulation". Polymorphism ============= Poly + Morphs Many + ways One thing ==> in many ways is called as "Polymorphism". Method1 ==>sum Method1 ==> difference Inheritance =========== process of creation of a new class from another class is called as "Inheritance". class Class1 class Class2 from Class1 Abstraction =========== process of showing the essential data and hiding the unessential data. Ex: InsurancePolicy: def calculateRiskFactor() ==> hide def showCoverage(): implemented ==> show HealthInsurance: def calculateRiskFactor(): VehicleInsurance: def calculateRiskFactor() ===================================================== class TermPolicy: age = 30 no alcohol no smoker software developer pay/month hide riskFactor() show TotalBenefit(): coverage = 8/25/50 subclass Health dad's age ==> 55 smoke no alcohol severity 15lacks ==> 8lacks subclass Wealth dad's age ==> 55 smoke no alcohol severity 15lacks ==> 8lacks ================================================================ Fundamentals of OOPs: ===================== class ===== ==> Logical Entity ==> Blueprint/Plan ==> Group/collection of attributes (data members/variables) and/or methods (behaviors) is called as "Class". ==> keyword: class we can create a class. Syntax: class ClassName: data members methods class MyFirstClass: # data members or attributes or variables name = "Ravi" age = 30 role = "Software Developer" Object ====== ==> Physical Entity ==> it is an instance of a class which can be used to access the members of the class. Syntax for object Creation: =========================== object-name = className() Syntax: objectname.meberName class MyFirstClass: # data members or attributes or variables name = "Ravi" age = 30 role = "Software Developer" # to access the data members of the class we should create an instance or object. # creation of object/instance mfc1 = MyFirstClass() print("The Data members of the given class = ") print(mfc1.name) print(mfc1.age) print(mfc1.role) ========================================================= Method ====== ==> When the class with more number of data members, to access these data members we need more complex code. ==> To simplify this we can use methods in class. Function Vs Method: ==================== function ==> def keyword ==> named block==> use to define certain block of operations ==> function can always write in outside the class. method ==> def keyword ==> named block ==> use to define certain block of operations ==> method can always write in class. Syntax: class ClassName: def methodName(self): # without parameters definition def methodName(self,p1,p2,p3,..):# with parameters definition def methodName(self): definition return value Here: self ==>keyword which describe the specified method belonging to the self class (same class). ==> for self ==> no value assignment required. class MyFirstClass: # data members or attributes or variables name = "Ravi" age = 30 role = "Software Developer" # defining a method def displayData(self): print("My Personal Details are:") print("Name = ",self.name) print("Age = ",self.age) print("Role = ",self.role) mfc = MyFirstClass() mfc.displayData() mfc1 = MyFirstClass() mfc1.displayData() =================================================== Constructor ============ # Parameterized Constructor =========================== class Employee: # constructor can always use for data initialization only. # constructor can be invoked/called at the time of object creation def __init__(self,name,age,role,salary): self.name = name self.age = age self.role = role self.salary = salary def displayData(self): print("Name = ",self.name) print("Age = ",self.age) print("Role = ",self.role) print("Salary = ",self.salary) emp1 = Employee("Ravi",30,"Software Developer","98765.0") emp1.displayData() emp2 = Employee("Ashish",35,"Tester",77654.0) emp2.displayData() ================================================== Default Constructor: ==================== ==> Constructor without parameters is called as "Default Constructor". ==> for the class, how Python Interpreter allow to create an object for any class? ================================================================================== Syntax: obj-name = ClassName() ==> at the time of creation of class, PVM (Python Virtual Machine/Interpreter) can add one default constructor. because of this, the PVM can allow for the creation of an object. class Student: # def __init__(self): # self.name = "Rakesh" # self.section = "10-A" # self.grade = "A+" name = "Rakesh" section = "10-A" grade = "A+" def displayData(self): print(self.name,self.section,self.grade) stu = Student() stu.displayData() Note: ==== We can allow only one constructor in class.