Docsity

Abstract Classes and Interfaces in Java: Purpose, Declaration, and Implementation, Study notes of Object Oriented Programming

Partial preview of the text

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

  • Subclasses must override

 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