Home » Posts tagged 'Python class inheritance'

Tag Archives: Python class inheritance

Newly Updated Posts

Python Inheritance

In this tutorial we, are going to learn Inheritance concept.

Inheritance is an important concept in object-oriented programming (OOP) that allows you to create new classes (derived classes) based on existing classes (base or parent classes). It is a way to establish a hierarchical relationship between classes, where the derived classes inherit the properties and behaviors of the base class.

The class from which a new class inherits is called the “base class” or “superclass,” and the class that inherits from the base class is called the “derived class” or “subclass.” Inheritance facilitates code reuse and the creation of hierarchical relationships between classes.

To define a class that inherits from another class, you specify the base class name in parentheses after the subclass name in the class definition. Here’s a basic syntax example

In Python, you can implement inheritance using the following syntax:

class BaseClass:
    # Base class attributes and methods

class DerivedClass(BaseClass):
    # Derived class attributes and methods

Here, DerivedClass is the derived class that inherits from BaseClass. The derived class inherits all the attributes (variables) and methods (functions) from the base class. It can also add new attributes or override existing methods from the base class to customize its behavior.

Let’s see an example:

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

    def speak(self):
        print("Animal speaks.")

class Dog(Animal):
    def __init__(self, name):
        super().__init__(name)

    def speak(self):
        print("Dog barks.")

class Cat(Animal):
    def __init__(self, name):
        super().__init__(name)

    def speak(self):
        print("Cat meows.")

# Creating instances of derived classes
dog = Dog("Buddy")
dog.speak()  # Output: Dog barks.

cat = Cat("Whiskers")
cat.speak()  # Output: Cat meows.

In this example, we have a base class Animal that has an attribute name and a method speak(). The derived classes Dog and Cat inherit from Animal. They both have their own __init__ method to initialize the name attribute, and they override the speak() method to provide their specific behavior.

When we create instances of Dog and Cat and call their speak() method, the appropriate implementation of the method in the derived class is executed.

Inheritance helps in code reuse, as you can define common attributes and methods in the base class and then extend or specialize them in the derived classes as needed. It promotes code organization, modularity, and flexibility in your program’s design.

Class,Object and Members

In this tutorial we will learn Class, Object and Members in Python.

In Python, classes are used to create objects, which are instances of those classes. Classes serve as blueprints or templates for creating objects with similar characteristics and behaviors. Each object created from a class is called an instance or an object of that class.

A class defines the properties (also known as attributes) and behaviors (also known as methods) that objects of that class will have. Attributes are variables that store data, while methods are functions that define the behavior of the class.

Here’s an example of a simple class definition in Python:

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

    def start_engine(self):
        print("The engine has started.")

    def drive(self):
        print("The car is being driven.")

    def stop_engine(self):
        print("The engine has stopped.")

In the above example, Car is the class, and make, model, and year are its attributes. The __init__ method is a special method called the constructor, which is executed when an instance of the class is created. It initializes the attributes of the object using the values passed as arguments.

The start_engine, drive, and stop_engine methods are behaviors associated with the Car class. They define the actions that objects of the Car class can perform.

To create an instance of the Car class, you can do the following:

my_car = Car("Toyota", "Camry", 2022)

Here, my_car is an object of the Car class. It has the attributes make (set to “Toyota”), model (set to “Camry”), and year (set to 2022). You can access the attributes and call the methods of the object using dot notation, like this:

print(my_car.make) # Output: Toyota

print(my_car.model) # Output: Camry

print(my_car.year) # Output: 2022

my_car.start_engine() # Output: The engine has started.

my_car.drive() # Output: The car is being driven.

my_car.stop_engine() # Output: The engine has stopped.

In summary, classes define the structure and behavior of objects, while objects are instances of those classes. Members refer to the attributes and methods associated with a class or an object. Attributes store data, and methods define the behaviors or actions that can be performed by the class or object.