Lesson 33 of 34

Inheritance: एक Class पर आगे बनाना

10 min 35 XP

एक class दूसरी class की सारी चीज़ें विरासत में ले सकती है, फिर नई चीज़ें जोड़ या बदल (override) सकती है। कोड दोबारा लिखने की ज़रूरत नहीं।

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

super() parent class का method बुलाता है। इसका उपयोग सबसे ज़्यादा __init__ में होता है।

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

OOP के चार स्तंभ: Encapsulation, Inheritance, Polymorphism और Abstraction। इंटरव्यू में इनके बारे में अक्सर पूछा जाता है!

Challenge

+35 XP

एक Shape class दी गई है। Rectangle(Shape) लिखें जिसमें __init__(width, height) और area() method (width × height) हो। फिर Square(Rectangle) लिखें जिसका __init__(side), super() के ज़रिए Rectangle को बुलाए।

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.