0% found this document useful (0 votes)
2 views31 pages

Inheritance and Polymorphism in Java

Uploaded by

Sahar Izzo
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)
2 views31 pages

Inheritance and Polymorphism in Java

Uploaded by

Sahar Izzo
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

CT038-3-2-OODJ & OBJECT ORIENTED DEVELOPMENT WITH JAVA

Inheritance and Polymorphism

Module Code & Module Title Slide Title SLIDE 1


Topic & Structure of the lesson

• Introduction
• Single Inheritance
• Multiple Inheritance
• Multi-level inheritance
• Inheriting Methods

Module Code & Module Title Slide Title SLIDE 2


LEARNING OUTCOMES

At the end of this topic, you should be able to:


– Implement inheritance
– Understand how to inherit and override superclass methods

Module Code & Module Title Slide Title SLIDE 3


Introduction

• Allows methods and fields to be shared amongst different


objects
• Eg: Vehicles include bicycles, skateboards, cars, jets (etc.)
share some common features and some that are unique to
each particular type
• Shared features are collected in a single class known as the
parent or superclass
• Unique features are separated into the child or subclasses

Module Code & Module Title Slide Title SLIDE 4


‹#›
Introduction

• Inheritance: extend existing classes by adding


methods and fields
• mechanism of deriving a new class from an old one
– old class = base class / super class / parent class
– new class = sub class / derived class / child class

Module Code & Module Title Slide Title SLIDE 5


‹#›
Introduction

• subclasses inherit all the variables and methods of


their parent classes
Module Code & Module Title Slide Title SLIDE 6
‹#›
Syntax : Inheritance

Module Code & Module Title Slide Title SLIDE 7


‹#›
Forms of Inheritance

• Inheritance may take the following forms:


– single inheritance
– multiple inheritance
– multi-level inheritance

Module Code & Module Title Slide Title SLIDE 8


‹#›
Single Inheritance

• Only one superclass

Module Code & Module Title Slide Title SLIDE 9


‹#›
Multiple Inheritance

• Several superclasses
• Not explicitly supported by Java
• Cannot use keyword extends
• Need to use Interface – keyword implements
– will be discussed in another lecture

Module Code & Module Title Slide Title SLIDE 10


‹#›
Multiple Inheritance

• Format

Module Code & Module Title Slide Title SLIDE 11


‹#›
Multi-level Inheritance

• Multiple levels of sharing


• Supervisor inherits from both Manager and
Employee

Module Code & Module Title Slide Title SLIDE 12


‹#›
Multi-level Inheritance

Module Code & Module Title Slide Title SLIDE 13


‹#›
Example:
Hierarchy of Bank Accounts

• Consider a bank that offers its customers the following


account types:
• Checking account:
– no interest
– small number of free transactions per month
– additional transactions are charged a small fee
• Savings account:
– earns interest that compounds monthly

Module Code & Module Title Slide Title SLIDE 14


‹#›
Example:
Hierarchy of Bank Accounts

• Inheritance hierarchy:
– All bank accounts support the getBalance method
– All bank accounts support the deposit and withdraw methods, but the
implementations differ
– Checking account needs a method deductFees
– savings account needs a method addInterest

Module Code & Module Title Slide Title SLIDE 15


‹#›
Example

Module Code & Module Title Slide Title SLIDE 16


‹#›
Example

Module Code & Module Title Slide Title SLIDE 17


‹#›
Example

• SavingsAccount automatically inherits all methods and instance fields of


BankAccount
• A superclass has state and behavior, and the subclass inherits them.
• One advantage of inheritance is code reuse

Module Code & Module Title Slide Title SLIDE 18


‹#›
Example:
Implementing the CheckingAccount Class

• Checking account:
– no interest
– small number of free transactions per month
• Free 5 transactions in a month
– additional transactions are charged a small fee
• If > 5 transactions, charge RM2 per transaction

Module Code & Module Title Slide Title SLIDE 19


‹#›
Example:
Implementing the CheckingAccount Class

Module Code & Module Title Slide Title SLIDE 20


‹#›
Example:
Implementing the CheckingAccount Class

• Each CheckingAccount object has


– Two instance fields:
balance (inherited from BankAccount)
transactionCount (new to CheckingAccount)
– Four methods:
getBalance() (inherited from BankAccount)
deposit(double amount) (overrides BankAccount method)
withdraw(double amount) (overrides BankAccount method)
deductFees() (new to CheckingAccount)

Module Code & Module Title Slide Title SLIDE 21


‹#›
Polymorphism

• Static Polymorphism
– Compile time
– overloading
• Dynamic Polymorphism
– Runtime
– overriding

Module Code & Module Title Slide Title SLIDE 22


‹#›
Overloading

• Supply a different implementation of methods having same


name
• Must have same name but different list of parameters
• Method is called based on the arguments

Module Code & Module Title Slide Title SLIDE 23


‹#›
Overloading

• Constructor overloading

Module Code & Module Title Slide Title SLIDE 24


‹#›
Overloading

• Method overloading

What if I define another run method


with different return type?

public int run(){/*implementation*/}

Module Code & Module Title Slide Title SLIDE 25


‹#›
Override Method

• Supply a different implementation of a method that


exists in the superclass
• Must have same name and same parameter types
• If method is applied to an object of the subclass type,
the overriding method is executed

Module Code & Module Title Slide Title SLIDE 26


26
Override Method

• Method overriding

Cannot override when the


methods
share different return type

Module Code & Module Title Slide Title SLIDE 27


‹#›
Override Method

• Method overriding

Cannot override when the


access
is weaker in subclass

Module Code & Module Title Slide Title SLIDE 28


‹#›
Override Method

Module Code & Module Title Slide Title SLIDE 29


‹#›
Review

• What is inheritance?
• What are the three forms of inheritance?
• Show an example of overriding a superclass

Module Code & Module Title Slide Title SLIDE 30


30
Q&A

Module Code & Module Title Slide Title SLIDE 31


‹#›

You might also like