Professional Documents
Culture Documents
Inheritance SR AZ3
Inheritance SR AZ3
What is inheritance
• Inheritance is one of the most important aspects of Object
Oriented Programming (OOP). The key to understanding
Inheritance is that it provides code re-usability. In place of writing
the same code, again and again, we can simply inherit the
properties of one class into the other.
• This, as you can imagine, saves a ton of time. And time is money in
data science!
2
What is Inheritance in OOP
• Inheritance is the procedure 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.
• The interesting thing is, along with the inherited properties and
methods, a child class can have its own properties and methods.
3
Terms used in Inheritance
• Class: A class is a group of objects which have common properties. It is a template or
blueprint from which objects are created.
• Sub Class/Child Class: Subclass is a class which inherits the other class. It is also
called a derived class, extended class, or child class.
• Super Class/Parent Class: Superclass is the class from where a subclass inherits the
features. It is also called a base class or a parent class.
5
Single Inheritance
• When a class inherits another class, it is known as
a single inheritance. In the example given below, Dog
class inherits the Animal class, so there is the single
inheritance.
6
Example:
7
Multilevel Inheritance
• When there is a chain of inheritance, it is known
as multilevel inheritance. As you can see in the
example given below, BabyDog class inherits the
Dog class which again inherits the Animal class, so
there is a multilevel inheritance.
8
Example:
9
Hierarchical Inheritance
• When two or more classes inherits a single class, it
is known as hierarchical inheritance. In the example
given below, Dog and Cat classes inherits the
Animal class, so there is hierarchical inheritance.
10
Example:
11
Types of inheritance in java
12
Q) Why multiple inheritance is not supported in java?
13
Example:
Output:
14
Super Keyword in Java
15
16
Example:
17
Example:
18
Example:
19
Benefits of Inheritance
• Inheritance helps in code reuse. The child class may use the code defined in the parent class
without re-writing it.
• Inheritance can save time and effort as the main code need not be written again.
• With inheritance, we will be able to override the methods of the base class so that the
meaningful implementation of the base class method can be designed in the derived class. An
inheritance leads to less development and maintenance costs.
• In inheritance base class can decide to keep some data private so that it cannot be altered by
the derived class.
20
Costs of Inheritance
• Inheritance makes the two classes (base and inherited class) get tightly coupled.
This means one cannot be used independently of each other.
• The changes made in the parent class will affect the behavior of child class too.
21
Reference:
• Inheritance in Java - Javatpoint
• Courses | Sololearn
• What is Multiple Inheritance? - Definition from Tech
opedia
22
Thanks!
Any questions?
23