0% found this document useful (0 votes)
10 views29 pages

Module 5 - Inheritance

The document provides an overview of inheritance in Java, explaining its significance in Object-Oriented Programming and detailing the roles of superclasses and subclasses. It outlines the five types of inheritance supported by Java, including Single, Multilevel, Hierarchical, Multiple (via interfaces), and Hybrid (also via interfaces). Additionally, it discusses the IS-A relationship as a fundamental concept in understanding inheritance in Java.
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)
10 views29 pages

Module 5 - Inheritance

The document provides an overview of inheritance in Java, explaining its significance in Object-Oriented Programming and detailing the roles of superclasses and subclasses. It outlines the five types of inheritance supported by Java, including Single, Multilevel, Hierarchical, Multiple (via interfaces), and Hybrid (also via interfaces). Additionally, it discusses the IS-A relationship as a fundamental concept in understanding inheritance in Java.
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

JAVA Inheritance

Topic Agenda
I. Introduction to Inheritance
II. Syntax of Inheritance in Java
III. Types of Inheritance
IV. Understanding the IS-A Relationship
Learning Objectives
By the end of this lesson, students should be able to:
1. Define inheritance and explain its role in Object-Oriented Programming
(OOP), specifically in Java.
2. Describe the purpose of a superclass and subclass, and explain how
properties and behaviors are passed from parent to child classes.
3. Identify and explain the five types of inheritance (Single, Multilevel,
Hierarchical, Multiple, Hybrid) as presented, including which ones Java
directly supports through classes and which are implemented using
interfaces.
4. Differentiate between each type of inheritance using simple examples
and diagrams.
5. Interpret syntax examples to understand how inheritance is
implemented in Java.
6. Explain the IS-A relationship and demonstrate how it represents
inheritance in Java through real-world or coding examples.
I. Introduction to
Inheritance
Inheritance
Inheritance in Java is a mechanism in which one object
acquires all the properties and behaviors of a parent object.

The idea behind inheritance in Java is that you can create


new classes that are built upon existing classes.

When you inherit from an existing class, you can reuse


methods and fields of the parent class. Moreover, you can
add new methods and fields in your current class also.
Why use Inheritance?

For Method Overriding (so runtime polymorphism can be


achieved).

For code Reusability


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.
Terms used in Inheritance

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.
Terms used in Inheritance

The extends keyword indicates that you are making a new class that
derives from an existing class. The meaning of "extends" is to
increase the functionality.
II. Syntax of
Inheritance in
Java
Syntax
Example:
Example:
III. Types of
Inheritance in
Java
Types of Inheritance

1. Single-Level Inheritance
2. Multi-Level Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance
1. Single Level Inheritance
In single inheritance, a sub-class is
derived from only one super class.
It inherits the properties and
behavior of a single-parent class.
Sometimes, it is also known as
simple inheritance.
Example:
2. Multilevel Inheritance
In Multilevel Inheritance, a derived
class will be inheriting a base class
and as well as the derived class also
acts as the base class for other
classes.
Example:
3. Hierarchical Inheritance
In hierarchical inheritance, more
than one subclass is inherited from
a single base class. i.e. more than
one derived class is created from a
single base class. For example, cars
and buses both are vehicle
Example:
4. Multiple Inheritance
In Multiple inheritances, one class
can have more than one superclass
and inherit features from all parent
classes.

Note: that Java does not support multiple


inheritances with classes. In Java, we can achieve
multiple inheritances only through Interfaces.
Example:
5. Hybrid Inheritance
It is a mix of two or more of the
above types of inheritance. In Java,
we can achieve hybrid inheritance
only through Interfaces if we want
to involve multiple inheritance to
implement Hybrid inheritance.
IV. Understanding
the IS-A
Relationship
Java IS-A type of Relationship
IS-A represents an inheritance
relationship in Java, meaning
this object is a type of that
object.
Java IS-A type of Relationship
Now, based on the above example, in
Object-Oriented terms, the following
are true:

• SolarSystem is the superclass of


Earth class.
• SolarSystem is the superclass of
Mars class.
• Earth and Mars are subclasses of
SolarSystem class.
• Moon is the subclass of both Earth
and SolarSystem classes.
Java IS-A type of Relationship
Now, based on the above example, in
Object-Oriented terms, the following
are true:

• SolarSystem is the superclass of


Earth class.
• SolarSystem is the superclass of
Mars class.
• Earth and Mars are subclasses of
SolarSystem class.
• Moon is the subclass of both Earth
and SolarSystem classes.
Example:

You might also like