Download Abstract Classes and Interfaces in Java: Purpose, Declaration, and Implementation and more Study notes Object Oriented Programming in PDF only on Docsity!
Object-Oriented
Programming:
Polymorphism
Chapter 10
What You Will Learn
ļµ What is polymorphism?
ļµ How to declare and use virtual
functions for abstract classes
Introduction
ļµPolymorphism
- Enables āprogramming in the generalā
- The same invocation can produce āmany formsā of results
ļµInterfaces
- Implemented by classes to assign common functionality to possibly unrelated classes
Polymorphism
ļµWhen a program invokes a method
through a superclass variable,
- the correct subclass version of the method is called,
- based on the type of the reference stored in the superclass variable
ļµThe same method name and signature
can cause different actions to occur,
- depending on the type of object on which the method is invoked
Polymorphism Promotes Extensibility
ļµ Software that invokes polymorphic
behavior
- independent of the object types to which messages are sent.
ļµ New object types that can respond to
existing method calls can be
- incorporated into a system without requiring modification of the base system.
- Only client code that instantiates new objects must be modified to accommodate new types.
Demonstrating Polymorphic Behavior
ļµA superclass reference can be aimed at
a subclass object
- a subclass object ā is-aā superclass object
- the type of the actual referenced object, not the type of the reference, determines which method is called
ļµA subclass reference can be aimed at a
superclass object only if the object is
downcasted
ļµView example, Figure 10.
Abstract Classes and Methods
ļµ Abstract classes
- Are superclasses (called abstract superclasses)
- Cannot be instantiated
- Incomplete ļµsubclasses fill in "missing pieces"
ļµ Concrete classes
- Can be instantiated
- Implement every method they declare
- Provide specifics
Abstract Classes and Methods
ļµ Purpose of an abstract class
- Declare common attributes ā¦
- Declare common behaviors of classes in a class hierarchy
ļµ Contains one or more abstract methods
ļµ Instance variables, concrete methods
of abstract class
- subject to normal rules of inheritance
Keyword abstract
ļµUse to declare a class abstract
ļµAlso use to declare a method
abstract
ļµAbstract classes normally contain
one or more abstract methods
ļµAll concrete subclasses must
override all inherited abstract
methods
Abstract Classes and Methods
ļµ Iterator class
- Traverses all the objects in a collection, such as an array
- Often used in polymorphic programming to traverse a collection that contains references to objects from various levels of a hierarchy
Beware! Compile Time Errors
ļµ Attempting to instantiate an object of
an abstract class
ļµ Failure to implement a superclassās
abstract methods in a subclass
- unless the subclass is also declared abstract.
Creating Abstract Superclass Employee
ļµabstract superclass Employee,
Figure 10.
- earnings is declared abstract ļµNo implementation can be given for earnings in the Employee abstract class
- An array of Employee variables will store references to subclass objects ļµearnings method calls from these variables will call the appropriate version of the earnings method
Polymorphic interface for the
Employee hierarchy classes.
Note in Example Hierarchy
ļµ Dynamic binding
- Also known as late binding
- Calls to overridden methods are resolved at execution time, based on the type of object referenced
ļµ instanceof operator
- Determines whether an object is an instance of a certain type