Docsity

Polymorphism, Abstract Classes and Interfaces: Java Programming Concepts, Lecture notes of Java Programming

Partial preview of the text

Download Polymorphism, Abstract Classes and Interfaces: Java Programming Concepts and more Lecture notes Java Programming in PDF only on Docsity!

LECTURE 07: ABSTRACT CLASS,

INTERFACE AND POLYMORPHISM

• Polymorphism

• Abstract class

• Interfaces

PLAN

Polymorphism

• What is polymorphism?

– poly = multiple

– morph = figure

Polymorphism

• Ability for objects

– of different classes

– related by inheritance

• Implemented with virtual functions

• When request made thru base-class pointer (or reference) to

use an overridden function

– Java knows enough to choose the correct function

To respond differently to same member function call

• With polymorphism, we can design and implement systems that

are easily extensible

– New classes can be added with little or no modification to the general

portions of the program => program processes generically.

– The only parts of a program that must be altered to accommodate new

classes

Polymorphism

• Once a class implements an interface, all objects of that class

have an is-a relationship with the interface type

• This is true of all subclasses of that class as well.

• Interfaces are particularly useful for assigning common

functionality to possibly unrelated classes

Polymorphism

  • Example: Quadrilaterals
    • If Rectangle is derived from Quadrilateral, then a Rectangle object is a more specific version of a Quadrilateral.
    • Any operation that can be performed on a Quadrilateral can also be performed on a Rectangle.
    • These operations can also be performed on other Quadrilaterals, such as Squares, Parallelograms and Trapezoids.
    • Polymorphism occurs when a program invokes a method through a superclass Quadrilateral variable—at execution time, the correct subclass version of the method is called, based on the type of the reference stored in the superclass variable.

Polymorphism Examples

  • In the next example, we aim a superclass reference at a subclass object.
    • Invoking a method on a subclass object via a superclass reference invokes the subclass functionality
    • The type of the referenced object, not the type of the variable, determines which method is called
  • This example demonstrates that an object of a subclass can be treated as an object

of its superclass

  • A program can create an array of superclass variables that refer to objects of

many subclass types.

Demonstrating Polymorphic Behavior

  • When a superclass variable contains a reference to a subclass object, and that

reference is used to call a method, the subclass version of the method is called.

  • When the compiler encounters a method call made through a variable, the

compiler determines if the method can be called by checking the variable’s class

type.

  • At execution time, the type of the object to which the variable refers determines

the actual method to use => This process is called dynamic binding.

Demonstrating Polymorphic Behavior

  • Classes that can be used to instantiate objects are called concrete classes.
  • Such classes provide implementations of every method they declare
  • Abstract superclasses are too general to create real objects—they specify only what is common among subclasses.
  • Concrete classes provide the specifics that make it reasonable to instantiate objects.
  • Not all hierarchies contain abstract classes.

Abstract Classes and Methods (Cont.)

  • Programmers often write client code that uses only abstract superclass types to reduce client code’s dependencies on a range of subclass types. - You can write a method with a parameter of an abstract superclass type. - When called, such a method can receive an object of any concrete class that directly or indirectly extends the superclass specified as the parameter’s type.
  • Abstract classes sometimes constitute several levels of a hierarchy.

Abstract Classes and Methods (Cont.)

  • Cannot instantiate objects of abstract superclasses, but you can use abstract superclasses to declare variables - These can hold references to objects of any concrete class derived from those abstract superclasses. - Programs typically use such variables to manipulate subclass objects polymorphically.

Abstract Classes and Methods (Cont.)

  • Use an abstract method and polymorphism to perform payroll calculations based

on the type of inheritance hierarchy headed by an employee.

  • Enhanced employee inheritance hierarchy requirements:
    • A company pays its employees on a weekly basis. The employees are of four

types: Salaried employees are paid a fixed weekly salary regardless of the

number of hours worked, hourly employees are paid by the hour and receive

overtime pay (i.e., 1.5 times their hourly salary rate) for all hours worked in

excess of 40 hours, commission employees are paid a percentage of their sales

and base-salaried commission employees receive a base salary plus a

percentage of their sales. For the current pay period, the company has decided

to reward salaried-commission employees by adding 10% to their base

salaries. The company wants to write a Java application that performs its

payroll calculations polymorphically.

Payroll System Using Polymorphism