What is inheritance in OOPs in python Explain it with example

This recipe explains what is inheritance in OOPs in python with example

Recipe Objective

This recipe explains what is inheritance for example.

Defining Inheritance

The characteristic of inheriting the properties from a parent class by a child class is termed inheritance. Inheritance is a fundamental oop concept. Inheritance allows us the reusability of code that is we do not have to write the same code, again and again, we can just simply inherit properties from the parent class that we need in our child class. Inheritance is applicable in the real-world model. It is transitive which is if a child class inherits properties from a parent class, then all the property of the parent class would be automatically inherited by other sub-classes of the child class.

Comprehensive List of Computer Vision Project Ideas for Data Enthusiasts

    class vehicle:
    
    def __init__(self,model,mileage,cost):
        self.cost = cost
        self.mileage = mileage
        self.model = model
        
    def details(self):
        print(f'Cost : {self.cost} ₹')
        print(f'Model : {self.model}')
        print(f'Mileage : {self.mileage}')

    class car(vehicle):        
    
        def rating(self):
        print('5 star')

    # Create Instance for Derived Class
    tata = car("Safari",25,2500000)

    # Call Method of Parent Class via Derived Class.
    tata.details() 

    # Call Method inside Derived Class
    tata.rating()  
    

Master the Art of Classification in Machine Learning to Become a Pro

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.