0% found this document useful (0 votes)
5 views19 pages

Chapter 6

Uploaded by

Akshara Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views19 pages

Chapter 6

Uploaded by

Akshara Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

6001104-3 Structured

Programming

L. Rafika Maaroufi

1
Chapter6:
Inheritance

2
Objectives

The notions of superclasses and


subclasses and the relationship
between them.
To use keyword extends to create a
class that inherits attributes and
behaviors from another class.

3
Outline

Inheritance in java
Superclasses and Subclasses
Relationship between Superclasses and
Subclasses

4
Inheritance in java
Inheritance in java :
is a mechanism in which one object acquires all
the properties and behaviors of a parent object

is one of the object-oriented programming (OOP)


primary capabilities.

is a mechanism for a modular and hierarchical


organization
allows the creation of hierarchical classifications

5
A hierarchical design

A hierarchy involving architectural buildings

6
Superclasses and Subclasses

In object-oriented terminology:
the existing class is typically described as
the base class, parent class, or superclass.

the newly defined class is known as the


subclass or child class.

7
Superclasses and Subclasses
Inheritance examples:

8
Superclasses and Subclasses

The subclass can differentiate itself from its


superclass in two ways:
It may augment the superclass by adding
new fields and new methods.

It may also specialize existing behaviors by


providing a new implementation that
overrides an existing method.

9
Relationship between
Superclasses and Subclasses
“is a” relationship : is a correspondence
between levels
Examples:
(1) a house is a
building
Superclass:
building
Subclass: house
(2) a ranch is a
house.
Superclass: house
Subclass: ranch
10
The “extends” keyword
Syntax of java Inheritance:
class Subclass-name extends Superclass-name
{
//methods and fields
}

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.

We say that the subclass extends the superclass.


11
Java Inheritance Example
The relationship between the two classes is:
In Java Programmer IS-A Employee

Employee
Salary: float

Programmer
bonus: int

12
Why use inheritance in java?
Method Overriding:
If subclass (child class) has the same method as declared in
the parent class, it is known as method overriding in Java.
In other words, it is used to provide the specific
implementation of a method which is already provided by
its superclass

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.
13
Example:
Java Program to demonstrate why we need method
overriding

calling the method of parent


class with child class object
14
Types of inheritance in java

There can be three types of inheritance in


java:
Class A Class A Class A

Class B Class C Class B Class B

hierarchical single

Class C

multilevel

15
Single Inheritance Example

16
Multilevel Inheritance Example

17
Hierarchical Inheritance
Example
I

18
References

Java: How to Program, 9e, Dietel and Dietel


, Pearson 0273759760
Data Structures and Algorithms in Java, 6th
edition, by M.T. Goodrich and R.
Tamassia. John Wiley and Sons, Inc., ISBN:
0-471-73884-0
Java The Complete Reference Ninth Edition

19

You might also like