0% found this document useful (0 votes)
4 views

Lecture 3

Uploaded by

Raj Gami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture 3

Uploaded by

Raj Gami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Inheritance & Polymorphism

|
Objectives
• Explain the concept of inheritance and its
significance.

• Explain the concept of polymorphism and its


significance.

• To implement inheritance and polymorphism in Java.

• Use access modifiers on classes.

• Explain how constructors are used in inheritance


hierarchies.
Friday,
September
22, 2017
OOP I | 2
Overview
• Subdivided into three sections:
– Inheritance: software reuse

– Polymorphism: Overriding Methods

– Effects of Inheritance: Constructors and


Access Modifiers

Friday,
September
22, 2017
OOP I | 3
Overview
• Inheritance: software reuse

• Polymorphism: Overriding Methods

• Effects of Inheritance: Constructors and


Access Modifiers

Friday,
September
22, 2017
OOP I | 4
Inheritance

|
Inheritance Definition
• Inheritance is a mechanism wherein a new
class is derived from an already existing
class. It is a feature that represents “is-a”
relationship between classes.
• A class derived from another class is called a
subclass, whereas the class from which a
subclass is derived is called a superclass.
• Every object of a subclass is also an object
of the superclass. But the reverse is not
true.
Friday,
September
22, 2017
OOP I | 6
Inheritance Examples

Superclass Subclass
Student GraduateStudent, UndergraduateStudent

Shape Circle, Triangle, Rectangle, Square

Loan CarLoan, MortgageLoan, EmergencyLoan

Employee Faculty, Staff

BankAccount FixedDepositAccount, SavingsAccount

Friday,
September
22, 2017
OOP I | 7
Inheritance Significance
• Inheritance is a form of software reuse: a new
class is created by absorbing an existing class’s
members and adding more capabilities. This allows
programmers to share code and software in the
form of classes packaged together.

• With inheritance, programmers save time during


program development by reusing proven and
debugged high quality software (classes).
Friday,
September
22, 2017
OOP I | 8
Inheritance Problem
• One problem with inheritance is that a
subclass can inherit methods that it does not
need or should not have.

• Sometimes methods need to be customized


to be appropriate. Polymorphism comes in
to solve this problem

Friday,
September
22, 2017
OOP I | 9
Class Demo

Friday,
September
22, 2017
OOP I | 10
Friday,
September
22, 2017
OOP I | 11
Friday,
September
22, 2017
OOP I | 12
Friday,
September
22, 2017
OOP I | 13
Class Work (15 Minutes)

Friday,
September
22, 2017
OOP I | 14
Overview
• Inheritance: software reuse

• Polymorphism: Overriding Methods

• Effects of Inheritance: Constructors and


Access Modifiers

Friday,
September
22, 2017
OOP I | 15
Polymorphism

|
Polymorphism Definition
• Polymorphism is an OOP concept that enables a sub class to
modify the behaviors (methods) of the super class to suite
its own convenience. This is process is known as overriding
behaviors.

• Therefore, the same method called by different objects has


“many forms” of results – hence the term polymorphism.

• The only condition is that the new classes must be part of


the inheritance hierarchy. Polymorphism is built on
inheritance.
Friday,
September
22, 2017
OOP I | 17
Polymorphism Significance
• With polymorphism, we can make design and
implement systems that are easily extensible.

• New classes can be added with little or no


modification to the general portions of the entire
program that uses the classes.

Friday,
September
22, 2017
OOP I | 18
Class Demo

Friday,
September
22, 2017
OOP I | 19
Friday,
September
22, 2017
OOP I | 20
Friday,
September
22, 2017
OOP I | 21
Friday,
September
22, 2017
OOP I | 22
Friday,
September
22, 2017
OOP I | 23
Class Work (15 Minutes)

Friday,
September
22, 2017
OOP I | 24
Overview
• Inheritance: software reuse

• Polymorphism: Overriding Methods

• Effects of Inheritance: Constructors and


Access Modifiers

Friday,
September
22, 2017
OOP I | 25
Effects of Inheritance

|
Effects of Inheritance
1. Constructors
2. Access modifiers

Friday,
September
22, 2017
OOP I | 27
Initializing superclass
members
• Superclass’s constructors are not inherited
by its subclasses.
• In order to initialize the instance variables
inherited from the superclass properly, each
subclass’s constructor should call the
superclass’s constructor either explicitly or
implicitly.
• keyword super is used to call the
superclass’s constructor
Friday,
September
22, 2017
OOP I | 28
Keywords: super and this

|
Keyword: super
• Members of a subclass (mostly methods) can
execute members of the superclass b
through the keyword super and a dot (.)
separator.
• When a subclass method overrides a
superclass method, the superclass method
can be accessed from the subclass by
preceding the superclass method name with
keyword super.

Friday,
September
22, 2017
OOP I | 30
Keyword: super & this
super this
Used to access members of the Used to access members of the
parent class (super-class) current class

Mostly used in overriding Mostly used in initialization


Suppose you have a function in Suppose the local variables of a
the current class (sub-class) that function have the same name as
has the same name and the instance variables of that
parameters as another function class. This is used to refer to the
in the parent class (super-class). instance variables of the class.
Super is used to refer to the
function in the parent class,
while this is used to refer to the
function in the current class.

Friday,
September
22, 2017
OOP I | 31
Access Modifiers and Inheritance

|
Protected Access Modifier

Class A Class B extends A Object of Class B


(Superclass) (Subclass)

Public Members Inherited and Contains and


Accessible Accessible
Protected Members Inherited and Contains but NOT
Accessible Accessible

Private Members Inherited but NOT Contains but NOT


Accessible Accessible

Friday,
September
22, 2017
OOP I | 33
Class Exercise (5 Minutes)
Write down the output of the following codes

Friday,
September
22, 2017
OOP I | 34
Class Exercise (5 Minutes)
Write down the output of the following codes

Friday,
September
22, 2017
OOP I | 35
Class Exercise (5 Minutes)

Identify the errors in


the following codes

Friday,
September
22, 2017
OOP I | 36
Class Exercise (5 Minutes)

Write down the output


of the following codes

Friday,
September
22, 2017
OOP I | 37
Assignment

• Differentiate between inheritance and


composition relationships

Friday,
September
22, 2017
OOP I | 38
References

• P.J. Deitel and H.M. Deitel (2007), Java How


to Program. 7th Edition. Pearson Education
Inc.

Friday,
September
22, 2017
OOP I | 40
Ole Sangale Road, Madaraka Estate. PO Box 59857-00200, Nairobi, Kenya
Tel: (+254) (0)703 034000/200/300 Fax : +254 (0)20 607498
Email: [email protected] Website: www.strathmore.edu
|

You might also like