What is Inheritance in Object-Oriented Programming? - Codecademy Blog (2024)

Inheritance is one of the core features of object-oriented programming. It’s a programming procedure that allows you to reuse code by referencing the behaviors and data of an object. In other words, a class that inherits from another class shares all the attributes and methods of the referenced class.

An inherited class is called a subclass or child class of the class it inherits from. And the class being inherited is called either a parent class, superclass, or base class.

Some languages, like Go, are object-oriented but use composition instead of inheritance. Composition is an alternative approach that creates new complex objects by aggregating other objects. Most object-oriented programming languages have both composition and inheritance.

Ahead, we’ll take a closer look at how inheritance comes in handy, the many types of inheritance you can implement, and other important details you’ll need to know.

Learn something new for free

  • Intro to ChatGPT
  • Learn SQL

How inheritance makes development easier

Inheritance is a core element of object-oriented programming that serves as a powerful instrument for reusing code. Let’s use an example to illustrate how using inheritance can make creating an application easier.

Say you’re designing a video game with vehicles you can drive. You want to create a lot of vehicles that people can use, including coupes, sedans, trucks, four-wheel-drive vehicles, and maybe even some airplanes.

If you’re already somewhat familiar with object-oriented programming, you might consider making all of these vehicles as objects. You could create a class for each of the types of vehicles you want and encapsulate all the functionality and data necessary for that vehicle in the class.

So you start by creating a class for a simple car. Let’s use Python for our examples. Python is a general-purpose programming language that is used for all types of projects, including data science, machine learning, web development, desktop application development, and scripting. It supports procedural and functional programming styles, as well as object-oriented programming. But back to the simple car class.

class Car: def init(self): self.wheels = 4 def drive(self, direction): print('car will drive ' + direction)

Of course, the class for Car could have many more attributes and methods than this, but you get the idea. But what if you wanted something a bit more creative —like a car with arms that could throw turtle shells or bananas (à la Mario Kart). You can add a throw method to the car class, but if a user has a car without arms, that wouldn’t apply.

You could try creating a copy of this class, renaming it, and adding all the methods and attributes —but if you planned on creating dozens of vehicles, that would take a lot of work. And if you changed one of the basic methods or attributes that affects all of your vehicles, you’d have to modify each one of these objects.

This is exactly where inheritance can help. Instead of copying the class and adding new things to it, you can create a new class that inherits from a base class. So for a car with arms, we could create a class like this:

class ArmedCar(Car): def throw(self, direction): print('car will throw to the ' + direction)

By inheriting from the base class of Car, you still get all the base functionality you need and you can add a throw method without affecting the original class. And if you ever want to change any of the base methods or attributes, you can do that in the base class and all of the child classes will be updated. Now let’s see how far we can take this concept by looking at the types of inheritance.

Types of inheritance

Many modern programming languages support the object-oriented programming paradigm, including JavaScript, Python, Java, PHP, C#, C++, Swift, and Ruby. Each of these languages handles inheritance slightly differently using different syntax, but most of the concepts remain the same. Not all languages support every one of these types of inheritance, but Python does.

Single inheritance

In single inheritance, one class inherits from only one parent class. This is also called simple inheritance because it’s the simplest type of inheritance you can use.

class Vehicle: def move(self): print('method to move vehicle called')class Motorcycle(Vehicle): def use_kickstand(self): print('method to use motorcycle kickstand called') motorcycle1 = Motorcycle() motorcycle1.move() # Prints "method to move vehicle called" motorcycle1.use_kickstand() # Prints "method to use motorcycle kickstand called"

The Motorcycle class inherits from the Vehicle class because it’s a type of vehicle and can use all the methods and class variables of the Vehicle class. This is the same type of inheritance we used in the first example.

Multiple inheritance

In Python and a lot of the other object-oriented programming languages, you can also create a class that inherits from more than one class. Here’s an example of multiple inheritance using animal traits:

class Animal: def breathe(self): print('breathe in and out')class Vertebrate: def bend_spine(self): print('bending spine')class Dog(Animal, Vertebrate): def pant(self): print('cooling off')dog1 = Dog() dog1.breathe() # Prints "breathe in and out" dog1.bend_spine() # Prints "bending spine" dog1.pant() # Prints "cooling off"

Multiple inheritance allows us to build up a class by inheriting from many classes.

Multilevel inheritance

In multilevel inheritance, one class inherits from another class, which inherits from yet another class, and on and on. Instead of using multiple inheritance for the Dog class from above, we could use multilevel inheritance.

class Animal: def breathe(self): print('breathe in and out')class Vertebrate(Animal): def bend_spine(self): print('bending spine')class Dog(Vertebrate): def pant(self): print('cooling off') dog1 = Dog() dog1.breathe() # Prints "breathe in and out" dog1.bend_spine() # Prints "bending spine" dog1.pant() # Prints "cooling off"

The results are the same for our simple case, but with more complex classes, one of the other types of inheritance will be more effective.

Hierarchical inheritance

In hierarchical inheritance, child classes all inherit from a single base class. It’s basically the same as single inheritance, except you’re creating more child classes. Going back to our video game example:

class Vehicle: def move(self): print('method to move vehicle called')class Motorcycle(Vehicle): def use_kickstand(self): print('method to use motorcycle kickstand called')class Skateboard(Vehicle): def ollie(self): print('method to do an ollie called') motorcycle1 = Motorcycle() skateboard1 = Skateboard() motorcycle1.move() # Prints "method to move vehicle called" motorcycle1.use_kickstand() # Prints "method to use motorcycle kickstand called" motorcycle1.ollie() # This will throw an error skateboard1.move() # Prints "method to move vehicle called" skateboard1.ollie() # Prints "method to do an ollie called" skateboard1.use_kickstand() # This will throw an error

Using hierarchical inheritance, each child class has all the functionality and data of the base class and also custom attributes and methods that apply only to that specific child class.

Hybrid inheritance

Hybrid inheritance involves using more than one of the other types of inheritance. When you know inheritance well and work on complex applications, chances are that this is the type of inheritance you will use most often to get the results you want.

class Vehicle: def move(self): print('method to move vehicle called')class Motorcycle(Vehicle): def use_kickstand(self): print('method to use motorcycle kickstand called')class Racing: def go_fast(self): print('method to go fast called')class RacingMotorcycle(Motorcycle, Racing): def win_cup(self): print('method to win cup called')

Access modifiers and inheritance

Object-oriented languages that use inheritance usually have a concept called access modifiers, which restrict access to variables and methods in a class. So while a child class inherits from a base class, you can limit the child class’ access to specific methods and variables in the base class. You can also limit access to these by using code outside of the class. Most object-oriented languages have three forms of access modifiers.

Public

These variables and methods can be accessed from anywhere in your application, whether it’s running inside or outside of the class. In Python, class methods and variables are public by default. In the examples above, all the methods and classes were public. Some of the other object-oriented programming languages have class methods and variables public by default. Others require using the public keyword before the method or variable, like in Java.

public class Car { public int wheels; public move() { } } 

Protected

Protected variables can only be accessed by a class (or classes) that inherit from it. In Python, you can make a variable or method protected by putting an underscore before the name. Many other languages use a protected keyword similar to the private keyword Java example above.

class Car: def init(self): self._wheels = 4 def _drive(self, direction): print('car will drive ' + direction)

Both the variable wheels and the method drive are protected. If Car is only going to be a base class and you’ll only use child classes to create objects, this makes sense.

Private

Private variables and methods are not intended to be accessed in the class. They can’t even be accessed by child classes. In Python, you can make a variable or method private by adding a double underscore to the front of its name. In most other languages, you have to use a private keyword.

class Car: def init(self): self.__wheels = 4 def __drive(self, direction): print('car will drive ' + direction)

In the code above, the variable wheels and the method drive are private. Note that the notation of protected and private variables is really more of a convention to highlight how a given object should be used. The use of underscores doesn’t really offer any security, ultimately any of these can be overridden.

Overriding and inheritance

When you’re creating a child class in most object-oriented programming languages, you also have the option to override the variables of a parent class in the child class. Below, the Vehicle class moves on the ground and the Helicopter class overrides the move method by flying in the air. The wheels variable also gets overridden in the child classes.

class Vehicle: def init(self): self.wheels = 4 def move(self): print('move on the ground')class Motorcycle(Vehicle): def init(self): self.wheels = 2class Helicopter(Vehicle): def init(self): self.wheels = 0 def move(self): print('take to the air') motorcycle1 = Motorcycle() motorcycle1.wheels # Prints "2" motorcycle1.move() # Prints "move on the ground" helicopter1 = Helicopter() helicopter1.wheels # Prints "0" helicopter1.move() # Prints "take to the air"

Learn more about inheritance

Inheritance can make object-oriented coding easier by adding a lot of flexibility to how you create objects and by giving you the ability to reuse your code. While this article gives you an overview of how inheritance works, it’s best to learn it in practice to see all the power it can bring to your coding projects.We have courses on object-oriented programming languages that will teach you inheritance, including Learn Python 3 and Learn JavaScript. And for a deep dive into inheritance, check out Java: Inheritance and Polymorphism.

Related courses

3 courses

Learn Java: Object-Oriented Programming Beginner Learn about object-oriented programming in Java.
Learn Intermediate Python 3: Object-Oriented Programming Intermediate Learn how to utilize one of the most popular programming paradigms – Object-Oriented Programming.

Subscribe for news, tips, and more

What is Inheritance in Object-Oriented Programming? - Codecademy Blog (2024)

FAQs

What is Inheritance in Object-Oriented Programming? - Codecademy Blog? ›

Inheritance is one of the most important principles of object-oriented programming (OOP). It allows for one class (child class) to inherit the fields and methods of another class (parent class). An inherited class is also called a subclass of its superclass (parent class).

What is inheritance in object-oriented programming? ›

A. Inheritance is the concept in OOPs in which one class inherits the attributes and methods of another class. The class whose properties and methods are inherited is known as the Parent class. And the class that inherits the properties from the parent class is the Child class.

What is inheritance in OOP source code? ›

Inheritance in OOP = When a class derives from another class. The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods. An inherited class is defined by using the extends keyword.

What do you mean by inheritance? ›

An inheritance is a financial term describing the assets passed down to individuals after someone dies. Most inheritances consist of cash that's parked in a bank account but may contain stocks, bonds, cars, jewelry, automobiles, art, antiques, real estate, and other tangible assets.

What is inheritance in OOP Quora? ›

Inheritance in Object-Oriented Programming (OOP) is a means by which child classes can share fields and methods of a parent class, and potentially extend upon them. I've made a silly example that will hopefully help you remember the principles: jshell> /list.

What is meant by inheritance and why it is important in object-oriented programming explain different types of inheritance supported in Java? ›

Java, Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones.

What is inheritance in object-oriented data model? ›

Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a conifer is a type of tree. There are certain characteristics that are true for all trees, yet there are specific characteristics for conifers.

How is inheritance explained? ›

How we inherit characteristics. Parents pass on traits or characteristics, such as eye colour and blood type, to their children through their genes. Some health conditions and diseases can be passed on genetically too. Sometimes, one characteristic has many different forms.

What is inheritance example easy? ›

Example 1: Java Inheritance

eat(); Here, labrador is an object of Dog . However, name and eat() are the members of the Animal class. Since Dog inherits the field and method from Animal , we are able to access the field and method using the object of the Dog .

What is inheritance in C++ in simple words? ›

Inheritance is a mechanism of reusing and extending existing classes without modifying them, thus producing hierarchical relationships between them. Inheritance is almost like embedding an object into a class. Suppose that you declare an object x of class A in the class definition of B .

What is inheritance and abstraction in OOP? ›

Inheritance and abstraction are two pivotal concepts in Object-Oriented Programming that empower developers to build scalable and maintainable software systems. Inheritance facilitates code reusability and promotes a hierarchical structure, while abstraction simplifies complex systems and enforces a high-level design.

What is inheritance in OOP Tutorialspoint? ›

One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.

What is inheritance and polymorphism in OOP? ›

The most basic difference between inheritance and polymorphism is that "inheritance" is a concept of objectoriented programming that allows creating a new class with the help of the features of an existing class, whereas the concept "polymorphism" represents multiple forms of a single function.

What is inheritance in C++ OOP? ›

Inheritance in C++ refers to the process wherein new classes can inherit properties (objects and methods) from pre-existing classes. There are multiple benefits to this, like inducing code reusability, modularity, easier maintenance, and more. Shivani Goyal. 06 Oct 2023.

What is inheritance in visual programming? ›

The Inherits statement is used to declare a new class, called a derived class, based on an existing class, known as a base class. Derived classes inherit, and can extend, the properties, methods, events, fields, and constants defined in the base class.

Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 6758

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.