Today is my 26th day of #100DaysOfCode and #python. Today I revised python basic, iterator, map, filter, reduce function, generator and class on python.
Tried to write some code on python class. Below is the code in class inheritance.
Inheritance is the capability of one class to derive or inherit the properties from another class. There are many benefits of inheritance it represents real-world relationships well. It provides reusability of a code. We don’t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it.
It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class . My simple code which I tried to write is given below.
class Classroom: def __init__(self,subject): self.subject = subject def getSubject(self): return self.subject def isCompulsory(self): return True def isOptional(self): return True class Compulsory(Classroom): def isCompulsory(self): return False class Optional(Classroom): def isOptional(self): return True sub1 = Classroom("Science") print(sub1.getSubject(),sub1.isCompulsory()) sub2 = Compulsory("Optional_Math") print(sub2.getSubject(),sub2.isCompulsory()) sub3 = Optional("Computer") print(sub3.getSubject(),sub3.isOptional())
When this code is run it give following output
Science True Optional_Math False Computer True