DEV Community

Durga Pokharel
Durga Pokharel

Posted on

Day 26 Of 100DaysOfCode: Class Inheritance

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.

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")

sub2 = Compulsory("Optional_Math")

sub3 = Optional("Computer")
Enter fullscreen mode Exit fullscreen mode

When this code is run it give following output

Science True
Optional_Math False
Computer True
Enter fullscreen mode Exit fullscreen mode

Day 26 of #100DaysOfcode and #Python
* More About Class
* Learned About Bubble Sort
* Revised python basic
* Class inheritance#100DayOfcode, #CodeNewbies ,#beginner ,#Python

— Durga Pokharel (@mathdurga) January 19, 2021

Top comments (0)