DEV Community

Durga Pokharel
Durga Pokharel

Posted on

1

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")
print(sub1.getSubject(),sub1.isCompulsory())

sub2 = Compulsory("Optional_Math")
print(sub2.getSubject(),sub2.isCompulsory())

sub3 = Optional("Computer")
print(sub3.getSubject(),sub3.isOptional())
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 pic.twitter.com/OBxFoNWcu4

— Durga Pokharel (@mathdurga) January 19, 2021

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay