From Java Programming II
Jump to: navigation, search

Polymorphism in Java

Learning Outcomes

At the end of this chapter, the student will be able to:

  • Understand the concept of polymorphism
  • To use overridden methods to effect polymorphism
  • To distinguish between abstract and concrete classes.
  • To declare abstract methods to create abstract classes.
  • How polymorphism makes systems extensible and maintainable.
  • To determine an object’s type at execution time.
  • To declare and implement interfaces

Lecture notes


  • Polymorphism: is defining a generic class that can be instantiated into different sub-classes (can take different shapes). This means we assign a specific-class object to a more generic-class object.
  • The advantage of polymorphism is to (1) allow programming from generic classes to more specific classes, (2) promote software reuse.
  • Polymorphic processing: It allows to process objects of different sub-classes as objects of a generic superclass.
    • We use instanceof to find the specific class type of a particular object during runtime.
    • We use downcasting to change the type of a generic class object to a specific class object.

This is an example of polymorphic processing:

      Employee cm = new CommissionEmployee("Mohamed", "Ali", "333-33-3333", 5000, 0.04);  
      Employee bpcm = new BasePlusCommissionEmployee ("Ahmed", "Mounr", "263-39-3355", 5000, 0.04, 300);
      Employee sm = new SalariedEmployee ("Kamel", "Mounir", "263-39-3344",300);

  ArrayList<Employee> employees = new ArrayList<Employee>();

    for (int i=0; i<employees.size();i++){
           System.out.printf("%.2f\n", employees.get(i).earning());
           if (employees.get(i) instanceof SalariedEmployee){ //finding the type in runtime
               SalariedEmployee sem = (SalariedEmployee)(employees.get(i)); //downcasting
               System.out.printf("weekly salary: %.2f\n", sem.getWeeklySalary());

Abstract Classes and Methods

  • Abstract method: a method that does not have an implementation. The method is only declared with its signature.
  • Abstract class: is a class that contains at least one abstract method. An abstract class cannot be instantiated. We cannot create an object from an abstract class because it contains a method that is not defined.
  • Example of abstract method:
public abstract double earning();

In the class Employee, we defined the method earning() as abstract because we cannot specify how this method should be implemented for a general employee. We do not know what is the salary of a general employee. We must know what is the type of the employee to be able to define the method earning(). The class Employee must be also defined as abstract class, because it contains an abstract method.

  • Why do we define an abstract method into a class?

This is to impose the sub-classes to override the abstract method. So, the abstract class can be seen as a contract that forces sub-classes to provide a specific implementation of each abstract method.


  • An interface is a generic class that is declared using the keyword interface and contains only abstract methods
  • An interface is defined as a general class that relates classes that are independent and do NOT have the is-a relationship. (Example: class Employee and class Invoice)
  • We use the keyword implements for a class to implement an interface.
  • A class that implements in interface MUST
    • Implement ALL the abstract methods of the interface (e.g. Invoice class) OR
    • be declared as an abstract class (e.g. Employee class)

In Java, a class can inherit only from ONE class (using extends) but it can implement more than one interface (using implements).

public class Invoice implements Payable, Comparable<Invoice>

In this case, you need to implement ALL the abstract methods of ALL the interfaces.

  • Comparable <T> is a special, known and useful interface in Java that allows to define a natural ordering between two objects of class T.
  • You can use it to define how to compare between two objects of the same class.
  • The class String is already comparable and its natural order is the alphabetic order.
  public int compareTo(Invoice invoice){
      return this.getPartNumber().compareTo(invoice.getPartNumber());



Polymorphism Part 01: Introduction

Polymorphism Part 02: Abstract Classes

Polymorphism Part 03: Abstract Classes and Interfaces

Polymorphism Part 04: Comparable Interface

Exams Solutions

Major 01 Spring 2016: Graph Problem