Lecture 3
Lecture 3
|
Objectives
• Explain the concept of inheritance and its
significance.
Friday,
September
22, 2017
OOP I | 3
Overview
• Inheritance: software reuse
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
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.
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
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.
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
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
Friday,
September
22, 2017
OOP I | 31
Access Modifiers and Inheritance
|
Protected Access Modifier
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)
Friday,
September
22, 2017
OOP I | 36
Class Exercise (5 Minutes)
Friday,
September
22, 2017
OOP I | 37
Assignment
Friday,
September
22, 2017
OOP I | 38
References
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
|