Inheritance in Java OOPs
Inheritance in Java OOPs
OOPs
What is Inheritance?
Single
Inheritance:
In Single
Inheritance one
class extends
another class (one
class only).
In above diagram, Class B extends only Class A. Class A is a
super class and Class B is a Sub-class.
Multiple Inheritance:
In Multiple Inheritance,
one class extending
more than one class.
Java does not support
multiple inheritance.
Multilevel Inheritance:
In Multilevel
Inheritance, one
class can inherit
from a derived
class. Hence, the
derived class
becomes the
base class for
the new class.
As per shown in diagram Class C is subclass of B and B is a of
subclass Class A.
Hierarchical Inheritance:
Syntax:
As displayed in the above figure, Programmer is the subclass and Employee is the superclass. The
relationship between the two classes is Programmer IS-A Employee. It means that Programmer is a
type of Employee.
In the above example, Programmer object can access the field of own class as well as of
Employee class i.e. code reusability.
Types of inheritance in java
Single Inheritance Example
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.
Program 1
Multilevel
Inheritance
Example
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. Program 2
Hierarchical Inheritance Example
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.
Program 3
Learn Inheritance in OOP’s with
Example
Consider a banking application
We are supposed to open two different account types, one for saving
and another for checking (also known as current).
Let's compare and
study how we can
approach coding from
a structured and
object-oriented
programming
perspective. Structural
approach: In structured
programming, we will
create two functions –
• One to withdraw
• And the other for
deposit action.
OOP's approach: While using the OOPs
programming approach. We would create two
classes.
• Each having implementation of the deposit and withdraw functions.
• This will redundant extra work.
Change Request in Software
What if the
requirement
changes further?
Like to add credit
card account with
its own unique
requirement of
deposits.
Structural approach: Using structural approach
you have to change tested piece of deposit code
again.
• OOP's approach: But using object-oriented approach, you will just
create a new class with its unique implementation of deposit method
( highlighted red in the image below).
• So even though the structural programming seems like an easy
approach initially, OOP's wins in a long term.
Advantage of Inheritance in
OOPs
But one may argue that across all classes, you have a repeated pieces of
code.
To overcome this, you create a parent class, say "account" and implement
the same function of deposit and withdraw. And make child classes
inherited "account" class. So that they will have access to withdraw and
deposit functions in account class.
To overcome this, you create a parent class, say "account" and implement
the same function of deposit and withdraw. And make child classes
inherited "account" class. So that they will have access to withdraw and
deposit functions in account class.
Program 5