לימוד פייתון :

CLASS INHERITANCE AND METHOD OVERRIDING

Goal

Learn how a base class passes data and behavior to child classes, how child classes add or change functionality, and how super() works.
This is essential for AI, robotics, and large software systems.


FULL CODE SNIPPET (COPY/PASTE INTO WORDPRESS)

############################################################
# LESSON 3: CLASS INHERITANCE AND METHOD OVERRIDING
#
# You will learn:
# 1. What is inheritance?
# 2. How a child class extends a parent class
# 3. How to override methods
# 4. How to use super()
# 5. How to organize clean code with main()
############################################################
import math

############################################################
# BASE CLASS: Shape
############################################################

class Shape:
def __init__(self, name):
self.name = name

def area(self):
"""
Parent class cannot compute area.
Child classes must override this method.
"""

raise NotImplementedError("Child class must implement area()")

def describe(self):
return f"This is a shape named {self.name}"

############################################################
# CHILD CLASS 1: Circle
############################################################

class Circle(Shape):
def __init__(self, radius):
# Call parent constructor
super().__init__("Circle")
self.radius = radius

def area(self):
# Override parent method
return math.pi * self.radius * self.radius

def describe(self):
# Extend base class describe() method
base = super().describe()
return f"{base} with radius {self.radius}"

############################################################
# CHILD CLASS 2: Rectangle
############################################################

class Rectangle(Shape):
def __init__(self, width, height):
super().__init__("Rectangle")
self.width = width
self.height = height

def area(self):
return self.width * self.height

def describe(self):
base = super().describe()
return f"{base} of size {self.width}x{self.height}"

############################################################
# MAIN PROGRAM
############################################################

def main():
print("=== LESSON 3: INHERITANCE DEMO ===\n")

circle = Circle(5)
rect = Rectangle(4, 7)

print(circle.describe())
print("Circle area:", circle.area(), "\n")

print(rect.describe())
print("Rectangle area:", rect.area())

print("\n=== END OF LESSON 3 ===")

############################################################
# RUN MAIN ONLY IF FILE EXECUTED DIRECTLY
############################################################

if __name__ == "__main__":
main()


LESSON EXPLANATION (TEXT FOR WORDPRESS)

1. What is inheritance?

Inheritance means one class (child class) receives code from another class (parent class).

Example:

class Circle(Shape):

Circle inherits:

  • name attribute

  • describe() method

so we don’t write them again.

2. Why use inheritance?

To reduce repeated code and create clean structures.

AI libraries (like PyTorch, TensorFlow) use inheritance everywhere:

  • Layer

  • Activation

  • Model

  • LossFunction

They all inherit a base class.

3. Overriding a method

Child classes can replace the parent’s method:

def area(self):
return math.pi * self.radius * self.radius

Circle overrides Shape.area().

4. Using super()

super() gives access to parent class functionality:

base = super().describe()

This allows the child to extend behavior:

return f"{base} with radius {self.radius}"

5. Structure with main()

main() organizes code professionally:

  • Clean separation of logic vs class definitions

  • Good for real projects

  • Good for importing into other modules