You are on page 1of 23

inheritance

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.

• Reusability: As the name specifies, reusability is a mechanism which facilitates you


to reuse the fields and methods of the existing class when you create a new class.
You can use the same fields and methods already defined in the previous class.
4
Types of inheritance in java

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?

• To reduce the complexity and simplify the language, multiple


inheritance is not supported in java.
• Examples of programming languages which support multiple
inheritance are C++, Python, Perl, Eiffel, Dylan, Curl, Eulisp and
Tcl. Java is one of the most prominent programming languages
which does not support multiple inheritance.

13
Example:

Output:

14
Super Keyword in Java

• The super keyword in Java is a reference variable which is used to refer


immediate parent class object.

• Whenever you create the instance of subclass, an instance of parent class is


created implicitly which is referred by super reference variable.

• Usage of Java super Keyword

• super can be used to refer immediate parent class instance variable.

• super can be used to invoke immediate parent class method.

• super() can be used to invoke immediate parent class constructor.

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.

• Inheritance provides a clear model structure which is easy to understand.

• An inheritance leads to less development and maintenance costs.

• 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.

• The overuse of inheritance makes the program more complex.

21
Reference:
• Inheritance in Java - Javatpoint
• Courses | Sololearn
• What is Multiple Inheritance? - Definition from Tech
opedia

22
Thanks!
Any questions?

23

You might also like