Lesson 33 of 34

Inheritance: பரம்பரைInheritance: Building on a Class

10 min 35 XP

ஒரு class மற்றொரு class-ன் அனைத்தையும் பெற்றுக்கொள்ளலாம் (inherit). பின்னர் புதியவற்றைச் சேர்க்கலாம் அல்லது மாற்றலாம் (override). இதனால் code மீண்டும் எழுத வேண்டியதில்லை.

A class can inherit everything from another class, then add or change (override) things. No need to rewrite code.

Example: edit me and press Run

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

super() மூலம் பெற்றோர் class-ன் method-ஐ அழைக்கலாம். பொதுவாக __init__-ல் பயன்படுத்துவோம்.

super() calls the parent class's method. It's most often used in __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. Interview-களில் இவை பற்றி அடிக்கடி கேட்பார்கள்!

The four pillars of OOP: Encapsulation, Inheritance, Polymorphism and Abstraction. Interviewers ask about them often!

Challenge

+35 XP

Shape class கொடுக்கப்பட்டுள்ளது. Rectangle(Shape) class எழுதுங்கள்: __init__(width, height) மற்றும் area() method (width × height). பிறகு Square(Rectangle) எழுதுங்கள்: __init__(side) மட்டும்; super() பயன்படுத்தி Rectangle-ஐ அழையுங்கள்.

A Shape class is given. Write Rectangle(Shape) with __init__(width, height) and an area() method (width × height). Then write Square(Rectangle) whose __init__(side) calls Rectangle through super().

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