PythonPlaza - Python & AI

Python

IntroductionVariablesIf-else
Strings Functions while-loop
For-LoopListSet
DictionaryTuple Try..Except
Class/ObjectInheritancePolymorphism
File Handling

Python inheritance

Inheritance is a very important concept of Object Oriented Programming. It allows a class to take on properties and behaviors of another class.

Parent Class:This class defines common attributes and methods.
Child Class: This class inherits from Parent Class.

Parent and Child Class

In inheritance, a child class (also called as subclass) automatically acquires the methods and attributes of parent class (also called as superclass). This allows the child class to reuse code from the parent. This reuse of code reduces redundancy and creates a specialized version of the parent class. An example would be a Cat class inheriting from an Animal class. A child class can also have its own new methods and variables or override existing ones from the parent class.

super() Function
super() function is used to call the parent class’s methods. It is commonly used in the child class’s __init__()

Key points in inheritance
1.) Inheritance lets child classes reuse parent class code.

2.) Child classes can add new methods.

3.) Child classes can override parent methods.

4.) It promotes code reuse, organization, and clean design.



Code Example 1:
#Print the numbers from 1 to 10..


#Bird is a Parent Class
class Bird:
    def __init__(self, name):
        self.name = name

    def flying(self):
        return f"{self.name} is flying!"

    def eating(self):
        return f"{self.name} is eating."

    def drinking(self):
        return f"{self.name} is eating."


#Child Class Parrot
#This class inherits from Bird, so it
#automatically gets .fly() and .eat().
#It can also have its own special behavior.

class Parrot(Bird):

    def __init__(self, name):
         super().__init__(name)
    def speaking(self):
        return f"{self.name} says: 'Hello!'"


#Child Class Crow
#This also inherits from Bird,
#but can override behaviors or 
#add new ones.


class Crow(Bird):
    def cawing(self):
        return f"{self.name} is cawing!"

    def flying(self):  # method overriding
        return f"{self.name} is flying high with strong wings!"



p = Parrot("Polly")
c = Crow("Blacky")

print(p.flying())     # Inherited from Bird
print(p.speaking())   # Parrot’s own method

print(c.flying())     # Crow's overridden fly method
print(c.cawing())     # Crow's own method

Output:
Polly is flying!
Polly says: 'Hello!'
Blacky is flying high with strong wings!
Blacky is cawing!



Types of Inheritance

All python classes have __init__() as a built-in method. It is always executed when the class is initiated. It is used to assign values to properties of the object, or perform operations that are necessary when an object is created from a class. It is called automatically whenever new object is created from a class.so, __init__() makes it easier to create objects and assign initial values to them.