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

An overview of abstract classes and methods in Java, their purpose, declaration, and implementation. It covers the concept of abstract classes as superclasses that cannot be instantiated and the role of concrete classes in filling in the missing pieces. The document also explains the use of interfaces to provide common methods for unrelated classes and the rules for implementing interfaces. Examples and case studies are included.

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

laurinda
laurinda šŸ‡¬šŸ‡§

4.8

(8)

220 documents

1 / 62

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object-Oriented
Programming:
Polymorphism
Chapter 10
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e

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