12/10/2018
1
Designed and Presented by
Dr. Ayman Elshenawy Elsefy
Dept. of Systems & Computer Eng..
Al-Azhar University
Website: eaymanelshenawy.wordpress.com
Email : [email protected]
Object Oriented Programming
Lecture 5
Inheritance and Polymorphism
Part 2
4
IN THIS Lecture, YOU WILL:
Learn about inheritance
Learn about subclasses and superclasses
Explore how to override the methods of a
superclass
Examine how constructors of superclasses and
subclasses work
Learn about polymorphism
Examine abstract classes
Become familiar with interfaces
Learn about composition
Objectives
5
Protected Members of class
Private members: are private to the class and cannot be
directly accessed outside the class. Only class methods can
access the private members directly.
The subclass cannot access the private members of the
superclass directly. ( sometimes it may be necessary for a
subclass to access a private member of a superclass).
Solution: make a private member public,
Errors: then anyone can access that member.
Another solution: Use Protected Modifier
any member of a superclass can be accessed directly
(only) by a subclass, that member is declared using the
modifier protected.
The accessibility of a protected member of a class falls
between public and private. A subclass can directly access the
protected member of a superclass.
12/10/2018
2
the protected instance variable can be
accessed directly by the methods. However, DClass objects cannot
directly access bCh. 9
12/10/2018
3
10
Dynamic Binding and Inheritance
• A committee that consists of four
people that are either students or
employees.
• Make the array of type Person so it can
accommodate any class derived fromit.
• we are assigning an object of a derived
class (either Student or
Undergraduate) to a variable defined
as an ancestor of the derived class
(Person).11
Dynamic Binding and Inheritance
• As a result, even though people[0]
is declared to be of type Person, the
method associated with the class
used to create the object is invoked.
This is called dynamic binding or
late binding.
• What Happen if we write:
12
Dynamic Binding and Inheritance
13
Dynamic Binding and Inheritance
Conclusion
when an overridden method is invoked, its action is the one
defined in the class used to create the object using the new
operator (not by the type of the variable naming the object).
A variable of any ancestor class can reference an object of a
descendant class, but the object always remembers which
method actions to use for every method name. The type of the
variable does not matter. What matters is the class name when
the object was created.
12/10/2018
4
14
Dynamic Binding and Inheritance
Output will be
15
Dynamic Binding with tostring()
Java uses dynamic binding with the toString
method.
The various println methods that belong to the
object System.out were written long before we
defined the class Student.
Interface and Abstract Classes Class interface consists of the headings for the public methods
and public named constants of the class, along with any
explanatory comments.
Knowing only a class’s interface, a programmer can write code
that uses the class (no need to know about the class’s
implementation).
Until now, Class interface is included in its definition.
Java Enables you to write class interface in a separate file
away from the implementation.
16
Interface and Abstract Classes A person calling his pets for dinner
Pets are able to be :
Named
Eat
Respond to command
17
Dog, Bird and Fish are 3 classes that implement these
behaviours
12/10/2018
5
Interface and Abstract Classes Java interface is a program component that contain the
headers and comments of public methods (All or subset) or
named constants.
You name and store an interface as you would a class
An interface has no instance variables, constructors, or method
definitions
To define the interface:
Use Implements ( one or more interfaces).
Define each method declared in the interface(s)
Several Classes can implement the same interface
18
Interface and Abstract Classes
19
Interface and Abstract Classes
20
Interface and Abstract Classes
21
12/10/2018
6
Interface as a type
22
Extending an Interface
A class that implements callable must be implement the method
come,setName, and getName
You can combine several interfaces into new one and add to
them new methods.23
Extending an Interface
24
Trainable will have setName, getName, come, respond, sit,
speak, lieDown
Class Object If a user-defined class include the definition of the function
toString() this function is executed when ever it is called.
If a user-defined class does not provide its own definition of the
method toString, then the default definition of the method
toString is invoked.
the default definition of the method toString returns the class
name followed by the hash code of the object.
You might ask, where is the method toString defined?
toString() defined in the class Object, and it is a public member.
If your defined class is not inherited from any class, the class
you define is automatically considered to be derived from the
class Object.
The class Object becomes the superclass of every class in
Java. 25
12/10/2018
7
class Object
26
Equivalent to
Constructors and Methods of the class Object
27
Java Stream Classes Hierarchy
28
Scanner Class Example
29
Scanner( InputStream )
This constructor create an object of Scanner class by talking an
object of InputStream class. An object of InputStream class is
called in which is created as a static data member in the System
class
12/10/2018
8
Scanner Class Example
30
Scanner Class Example
31
Input Stream Reader
32
turning the byte based input stream into a character based
Reader.
Java InputStreamReader interprets the bytes of a Java
InputStream as text instead of numerical data.
is often used to read characters from files (or network
connections) where the bytes represents text .
PrintWriter
33
Java PrintWriter class is the implementation of Writer class. It is
used to print the formatted representation of objects to the text-
output stream.
12/10/2018
9
Polymorphism
34
Polymorphism
35
In a class hierarchy, several methods may have the same name
and the same formal parameter list.
A reference variable of a class can refer to either an object of its
own class or an object of its subclass.
A reference variable can invoke, that is, execute, a method of
its own class or of its subclass(es).
Binding means associating a method definition with its
invocation, (determine which method definition gets executed).
Early binding, a method’s definition is associated with its
invocation when the code is compiled.
Late binding, a method’s definition is associated with the
method’s invocation at execution time, that is, when the
method is executed (java).
Term polymorphism means associating multiple (potential)
meanings with the same method name.
Final
36
If a method of a class is declared final, it cannot be overridden
with a new definition in a sub-class.
If a class is declared final, then no other class can be derived
from this class; that is, it cannot be the superclass of any other
classes.
Final
37
12/10/2018
10
Instance of Operator
38
To determine whether a reference variable that points to an
object is of a particular class type, Java provides the operator
instanceof.
p instanceof BoxShape
Abstract Methods and Classes
39
An abstract class is a class that is declared with the reserved
word abstract in its heading.
An abstract class is a class that is declared with abstract
keyword.
An abstract method is a method that is declared without an
implementation.
An abstract class may or may not have all abstract methods.
Some of them can be concrete methods
A method defined abstract must always be redefined in the
subclass,thus making overriding compulsory OR either make
subclass itself abstract.
Any class that contains one or more abstract methods must
also be declared with abstract keyword.
There can be no object of an abstract class.That is, an abstract
class can not be directly instantiated with the new operator.
An abstract class can have parametrized constructors and
default constructor is always present in an abstract class.
Example of Abstract Classes
40
You must define the abstract method in all inherited class
Example of Abstract Classes
41
12/10/2018
11
Banking Example
42
Banks offer various types of accounts Savings account :
InterestMinimum Balance
number of monthly checking writing
Monthly service charge
NoNoLimitedYesType 1
LowerLowerUn-LimitedNoType 2
HigherHigherUn-LimitedNoType 3
Certificate of deposit (CD) is used to save money for the long term
InterestMinimum Balance
LowerNoType 1
HigherYesType 2
Checking accounts:
money is left for some time and for higher interest ratesSuppose that you purchase a CD for six months. Then we say that the CD will
mature in six months. Furthermore, the penalty for early withdrawal is stiff.
inheritance hierarchy of these bank accounts.
43
abstract.
abstract.No abstract.
No abstract.
No abstract.No abstract. No abstract.
No abstract.
name,
accountNumber balance
inheritance hierarchy of these bank accounts. inheritance hierarchy of these bank accounts.
12/10/2018
12
inheritance hierarchy of these bank accounts. inheritance hierarchy of these bank accounts.
inheritance hierarchy of these bank accounts. inheritance hierarchy of these bank accounts.
12/10/2018
13
inheritance hierarchy of these bank accounts. inheritance hierarchy of these bank accounts.