Chapter-Three (Inheritance)
Chapter-Three (Inheritance)
Chapter-Three (Inheritance)
Undergraduate Program
Course Code: SE132
Course Title: Object Oriented
Programming
Chapter 3: Inheritance
Contents
Introduction
Super-classes and Sub-classes
Protected members
Overriding methods
Using this and super
Use of the final keyword with inheritance
Constructors in sub-classes
Introduction
Inheritance is one of the key features of OOP that allows us to create
a new class from an existing class.
– It is the ability to derive something specific from something generic.
A class can inherit the features of another class and add its own
modification
– The existing class is called the parent class, or superclass, or base class
– The derived class is called the child class or subclass
Cont...
As the name implies, the child inherits characteristics of the parent
– That is, the child class (subclass) inherits all the properties and methods of
the parent (super) class
Proper inheritance creates an is-a relationship, meaning the child is a
more specific version of the parent
Vehicle
Car
Is-a Relationship
In Java, inheritance is an is-a relationship. That is, we use inheritance
only if there exists an is-a relationship between two classes.
For example,
– Car is a Vehicle
– Orange is a Fruit
– Surgeon is a Doctor
– Dog is an Animal
Here, Car can inherit from Vehicle, Orange can inherit from Fruit,
and so on.
Why Inheritance?
The most important use of inheritance in Java is code reusability.
– The code that is present in the parent class can be directly used by the child
class.
Method overriding is also known as runtime polymorphism.
– Hence, we can achieve Polymorphism in Java with the help of inheritance.
Types of Inheritance
There are four types of inheritance.
– Single inheritance
– Multiple inheritance
– Hierarchical inheritance
– Multilevel inheritance
Cont...
Single Inheritance
– In single inheritance, a single subclass extends from a single superclass.
– For example,
Cont...
Multilevel Inheritance
– In multilevel inheritance, a subclass extends from a superclass and then the
same subclass acts as a superclass for another class.
– For example
Cont…
Hierarchical Inheritance
– In hierarchical inheritance, multiple subclasses extend from a single
superclass.
– For example,
Cont…
Multiple Inheritance
– In multiple inheritance, a single subclass extends from multiple super
classes.
– Note: Java doesn't support multiple inheritance
– For example,
Super and Sub Classes
The aim of inheritance is to provide the reusability of code so that
– A class has to write only the unique features and
– Rest of the common properties and functionalities can be extended from the
another class.
Sub class
– The class that extends the features of another class is known as child
class, sub class or derived class.
Super class
– The class whose properties and functionalities are used(inherited) by
another class is known as parent class, super class or Base class.
extends keyword
The extends keyword is used to perform inheritance in Java.
Here class Car is child class and class Vehicle is parent class.
The class Car is inheriting the properties and methods of Vehicle class.
class Car extends Vehicle {
}
Example
// method signatures