[go: up one dir, main page]

0% found this document useful (0 votes)
3 views6 pages

Inheritance

Inheritance in programming allows a child class to inherit attributes and methods from a parent class, facilitating code reuse and reducing redundancy. It can be categorized into single inheritance, multilevel inheritance, and multiple inheritance, each defining different relationships between classes. The concept is analogous to genetic inheritance, where offspring inherit traits from their parents while also being able to introduce new characteristics.

Uploaded by

gillgurman491
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Inheritance

Inheritance in programming allows a child class to inherit attributes and methods from a parent class, facilitating code reuse and reducing redundancy. It can be categorized into single inheritance, multilevel inheritance, and multiple inheritance, each defining different relationships between classes. The concept is analogous to genetic inheritance, where offspring inherit traits from their parents while also being able to introduce new characteristics.

Uploaded by

gillgurman491
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Inheritance

in programming
Vehicle

Car Motorcycle
Chapter 13
DEFINITION
What is meant by
‘Inheritance’?
Inheritance makes it possible to create a new
child class that inherits the attributes and
methods of the original (parent) class.

Inheritance can be used when you observe a


‘kind-of’ or ‘is-a’ relationship between classes.
You can say, for example, that “a dog is an
animal” or “a dog is a kind of animal”

TERMINOLOGY
The term ‘parent class’ is also referred to as
superclass or base class, while ‘child classes’ can
also be referred to as subclasses or derived
classes.
Purpose
Inheritance is a technique that helps reduce
repeated code. Code that is inherited from a
parent class does not need to be redefined. This
means that if the implementation detail needs
to change, it only needs to be changed in one
place (and all child classes will access the new
implementation).
Animal

Non-flying bird Flying Bird Reptile


Understanding
Penguin Ostrich Owl Snake Lizard Inheritance
Inheritance is an OOP technique
The high-level diagram shows us the names of the classes and the 01 where child classes are derived from
relationships between them. You can see that a penguin is a non- a parent class and inherit all of the
flying bird and that a snake is a reptile. attributes and methods from the
parent.

class Animal: The code in python shown to the left is


def speak(self): an example of an application of These properties don’t need to be
print("Animal sound") inheritance. 03 redefined for the child - the child
shares the code from the parent
class Reptile(Animal): ‘Animal’ class has a ‘speak’ method
pass that prints "Animal sound".
class.

class Snake(Reptile): ‘Snake’ class inherits from ‘Animal’


def speak(self): but overrides the ‘speak’ method to
print("Hiss") print "Hiss".

# usage Snake().speak() creates a ‘Snake’


snake = Snake() object and calls its ‘speak’ method,
snake.speak() # Output: Hiss which uses the overridden version.
Multiple inheritance is when a child class
MULTIPLE inherits from two or more (multiple) parent A B
classes.

Types of C
Inheritance MULTILEVEL
Multilevel inheritance is the process of
deriving a class from another (already)
derived class. When one class inherits
another class it is further inherited by
another class.

Single inheritance, or simple


SINGLE inheritance is when a child class
inherits the methods and
attributes of one and only one
single-parent class.

A Parent Class

C Child Class
Real-Life Analogy

Programming Inheritance vs. Genetic Inheritance


Inheritance in programming can be compared to Genetic Inheritance in Biology. Just as offspring inherit
genetic traits from their parents, such as physical features and behavioural traits, a child class inherits
attributes and methods (properties and behaviours) from a parent class.

A parent class is similar to a parent in Genetics, since this class contains fundamental traits (attributes and
methods) that can be passed down.

A child class is like an offspring, this class inherits the traits of the parent but can also introduce new
characteristics or override existing ones, much like how a child may express a variation of inherited genetic
traits.

The reuse of code that comes with inheritance is similar to Evolution in Biology, where beneficial traits are
inherited and passed down, allowing both organisms and programs to remain efficient and adaptable over
time.

You might also like