Posts

Showing posts with the label Python - Inheritance

Python Object Oriented Programming - Inheritance

Image
Inheritance is a fundamental concept in object-oriented programming. It allows you to reuse properties and methods of an existing class to create a new one.  Python makes inheritance quite simple and intuitive.  In this post, we will discuss the basics of inheritance with Python examples. Basic Inheritance Concepts   Inheritance is the ability of a class (child) to inherit attributes and methods from another class (parent) to avoid code duplication and simplify changes and updates. Creating a Parent Class First, let's design a base class called Animal that will have common attributes and methods for all animals. class Animal:     def __init__(self, name, age):         self .name = name         self .age = age       def sound(self):         print( f " { self .name} makes a sound." ) Creating a Child Class Now, let’s make a class called "Dog" that inherits the class Animal’s properties and methods. We do this by putting the parent class name in parentheses aft