OOPs Part-04: ============= Inner Classes ============= ==> writing of a class inside another class is called as an "Inner Classes". Ex: car without engine car with engine car ==> class engine ==> class BankSystem==> Main Class Withdraw ==> class deposit ==> class loans ==> class ==> Without existing one type of object if there is no chance of existing another type of object, we should use "inner class" class OuterClass: # main class def __init__(self):# constructor print("This is the Outer Class Constructor.") class InnerClass:# inner class def __init__(self): # constructor print("This is the Inner Class Constructor.") def m1(self):# method print("This is inner class Method.") # crating the object for outer class outer_obj = OuterClass() # creating the object for inner class inner_obj = outer_obj.InnerClass() inner_obj.m1() ======================================================== class Bank: def __init__(self,accountNumber,nameHolder): self.accountNumber = accountNUmber self.nameHolder = nameHodelr class Deposit: def __init__(self,balance): this.balance = balance def depositMoney(self,amount): if amount > 0: this.balance += amount else: print("Deposit is not possible with negative amount.") ============================================================= Garbage Collection: =================== C ==> Dynamic Memory allocation arrays[100] ==> store with 10 elements resize the memory, we should use "dynamic memory allocation". malloc() ==> started closed when not required. ==> Garbage collection in python ==> automatic import gc print(gc.isenabled()) # disable the garbage collection gc.disable() print(gc.isenabled()) # enable the garbage collection gc.enable() print(gc.isenabled()) ================================================== Destructors: ============ __init__() ==> for initializing the class data members. __del__() ==> destructor. Garbage collection ==> automatic i every python program when the object for the class can be created ==> garbage collection ==> enabled when the program is completed, the object need to destroy In this, the Garbage collector can call the destructor to perform the cleanup the activities. import time class Test: def __init__(self): print("Object Initalization...") def __del__(self): print("Fulfilling Last wish and performing cleanup activities...") t1 = Test() print(id(t1)) t1 = None time.sleep(10) print("End of application") print(id(t1)) =============================================================== Inheritance: ============ procedure of creating the new class from existing class is called as "Inheritance". new class ==> child class/sub class/derived class existing class ==> Parent class/super class/Master class ==> 5-types of inheritances: 1) Single Inheritance 2) Multi-level Inheritance 3) Multiple Inheritance 4) Hierarchy Inheritance 5) Hybrid Inheritance 1) Single Inheritance ===================== class Parent: def __init__(self,a,b): self.a = a self.b = b def m1(self): print("Hello, welcome to Python") class Child(Parent): def m2(self): print("This is the Single Inheritance.") cobj = Child(121,"Python") cobj.m2() cobj.m1() ========================================================== 2) Multi-level Inheritance ========================== class ClassA: def m1(self): print("Good Morning") class ClassB(ClassA): def m2(self): print("Good Afternoon") class ClassC(ClassB): def m3(self): print("Good Evening.") cobj = ClassC() cobj.m3() cobj.m2() cobj.m1() bobj = ClassB() # bobj.m3() bobj.m2() bobj.m1() ========================================================= 3) Multiple Inheritance ======================== class ClassA: def m1(self): print("This is the one of the Parent class implementation for multiple inheritance.") class ClassB: def m2(self): print("This is another parent class for multiple inheritance implementation.") class ClassC(ClassA,ClassB): def m3(self): print("I am a common child for ClassA and ClassB.") obj = ClassC() obj.m3() obj.m2() obj.m1() aobj = ClassA() # aobj.m3() # aobj.m2() aobj.m1() Note: ==== 1) Private data is visible within the same class only. (We cannot extend or make visible the private data in outside the class).