Table of Contents

Inheritance, Method Overriding and Data Hiding in Python

Table of Contents

Inheritance is a fundamental concept in object-oriented programming that allows you to define a new class based on an existing class. The new class is called the subclass, and the existing class is called the superclass.

In Python, you can create a subclass by specifying the superclass in the class definition. The subclass inherits all the attributes and methods of the superclass, and can add its own attributes and methods or override the attributes and methods of the superclass.

Here’s an example:

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

    def speak(self):
        print("An animal makes a sound.")

class Dog(Animal):
    def speak(self):
        print("The dog barks.")

class Cat(Animal):
    def speak(self):
        print("The cat meows.")

In this example, we define a superclass called Animal that has an __init__ method to set the name attribute and a speak method to print a generic animal sound. We also define two subclasses called Dog and Cat that inherit from the Animal superclass and override the speak method to print a specific sound for each animal.

To create an instance of the Dog class, we can call the __init__ method with a name argument:

my_dog = Dog("Rufus")

We can then call the speak method on the my_dog instance:

my_dog.speak()
# Output: The dog barks.

This code calls the speak method on the my_dog instance, which is a Dog object. Since the Dog class overrides the speak method, it prints “The dog barks.” instead of the generic animal sound.

In summary, inheritance is a way to create new classes based on existing classes in Python. Subclasses inherit all the attributes and methods of the superclass, and can add their own attributes and methods or override the attributes and methods of the superclass.


Method Overriding in Python

Method overriding is a concept in object-oriented programming that allows a subclass to provide a different implementation of a method that is already defined in its superclass. The subclass can use the same method name as the superclass, and the implementation in the subclass will be used instead of the implementation in the superclass.

In Python, you can override a method in a subclass by defining a method with the same name as the method in the superclass. Here’s an example:

class Animal:
    def make_sound(self):
        print("An animal makes a sound.")

class Dog(Animal):
    def make_sound(self):
        print("The dog barks.")

In this example, we define a superclass called Animal with a make_sound method that prints a generic animal sound. We also define a subclass called Dog that overrides the make_sound method to print “The dog barks.” instead of the generic animal sound.

To create an instance of the Dog class, we can call the make_sound method:

my_dog = Dog()
my_dog.make_sound()
# Output: The dog barks.

This code creates an instance of the Dog class and calls the make_sound method. Since the Dog class overrides the make_sound method, it prints “The dog barks.” instead of the generic animal sound.

In summary, method overriding is a way to provide a different implementation of a method in a subclass that is already defined in its superclass. In Python, you can override a method in a subclass by defining a method with the same name as the method in the superclass. When the method is called on an instance of the subclass, the implementation in the subclass will be used instead of the implementation in the superclass.


Data Hiding in Python

In Python, data hiding is a mechanism that allows you to restrict access to the attributes and methods of a class. This is achieved by prefixing the attribute or method name with two underscores (__) to make it private. Private attributes and methods are not visible to the outside world, and can only be accessed by the class itself.

Here’s an example:

class Car:
    def __init__(self, make, model, year):
        self.__make = make
        self.__model = model
        self.__year = year

    def get_make(self):
        return self.__make

    def get_model(self):
        return self.__model

    def get_year(self):
        return self.__year

In this example, we define a Car class with private attributes for make, model, and year. We also define public methods called get_make, get_model, and get_year to retrieve the values of these attributes. The __ prefix makes the attributes private and inaccessible from outside the class.

To create a new instance of the Car class, we can call the __init__ method with the make, model, and year arguments:

my_car = Car("Honda", "Civic", 2021)

We can then call the public methods to retrieve the values of the private attributes:

print(my_car.get_make())
# Output: Honda

print(my_car.get_model())
# Output: Civic

print(my_car.get_year())
# Output: 2021

This code retrieves the values of the private make, model, and year attributes using the public methods. Since the attributes are private, they cannot be accessed directly from outside the class.

In summary, data hiding is a mechanism in Python that allows you to restrict access to the attributes and methods of a class. Private attributes and methods are not visible to the outside world, and can only be accessed by the class itself. The __ prefix is used to make attributes and methods private.

Category
Tags

Copyright 2023-24 © Open Code