Lecture 6 - Polymorphism - 2
Lecture 6 - Polymorphism - 2
International University
School of Computer Science and Engineering
Polymorphism
(IT069IU)
Nguyen Trung Ky
🌐 it.hcmiu.edu.vn/user/ntky
1
Previous lecture
- Inheritance
- Definition and Examples
- Types of Inheritance
- UML Diagram
- Animal Inheritance Example
- Without Inheritance
- With Inheritance
- Method Overriding
- Constructors in Subclasses
- Keyword Super
- Method Overriding
- Keyword Super
- Access Modifier (Protected)
- Overloading
- Method Overloading
- Constructor Overloading
- Final Keyword
- Constant Variable
- Static Keyword
- Static Variable 2
- Static Method
Agenda’s today
- Polymorphism
- Method overriding and overloading in Inheritance
- Zoo Example
- Company Payroll Example
- Abstraction
- Abstract Class
- Abstract Method
- Why we need abstract class
- Examples:
- Zoo Example
- Company Payroll Example
- Interface
- Interface in real life examples
- Upgrade Company Payroll with Invoices Example
- Abstract vs Interface
3
Polymorphism
- Polymorphism literally means “many forms”.
- Polymorphism allows us to perform a single action in different ways. The same method is called in
different types of objects has different results. We can perform polymorphism in java by method
overloading and method overriding.
- Polymorphism enables you to write programs that process objects of subclasses that share the
same superclass as if they’re all objects of the superclass; this can simplify programming.
Animal
-name
-age
+speak()
+speak()
6
Superclass Animal
7
Class Dog
8
Class Cat
9
Class Duck
10
Class ZooPolymorphism for Testing
Output:
11
Company Payroll Example
- A company has many employees. Each employee can be:
- Developer
- Designer
- Manager
- Every employee will has a fixed base salary but each type of employee can
have a different way to have bonus to be added for their salary:
- Developer:
- Base Salary + How many projects he did * The bonus for each project.
- Designer:
- Base Salary + 13th Salary Month if that person does a good job.
- Manager:
- Base Salary + How big is the team they manage * The bonus to manage each person.
a. Write all necessary classes.
b. Write a Test class which creates three objects: developer, designer and
manager and print out all information from these objects.
12
UML Diagram for Company Payroll
13
Let’s live code in Java!
14
Superclass Employee
15
Class Developer
16
Class Designer
17
Class Manager
18
Class Company for Testing
Output:
19
Why do we need abstract class
20
Abstraction
21
Abstraction
- An abstract class is a class that contains one or more methods that do not
have any implementation provided.
- For example that you have an abstract class called Shape. It is abstract
because you cannot instantiate (create) it.
- If you ask someone to draw a shape, the first thing the person will most likely ask you is,
“What kind of shape?” Thus, the concept of a shape is abstract.
- However, if someone asks you to draw a circle, this is easier because a circle is a
concrete concept. You know what a circle looks like. You also know how to draw other
shapes, such as rectangles.
22
Abstraction in Shape
The class Shape does not provide any implementation for draw() ; basically there is no code, and this is
what makes the method abstract (providing any code would make the method concrete).
We want the subclasses to provide the implementation. Let’s look at the Circle and Rectangle classes:
- With abstract class, we can indicate that class should never be used to
create object directly. Also, the abstract class can provide abstract methods
for other classes to inherit it to provide specific implementation. 26
Abstract Class Zoo
[Info] The class Animal contains one abstract
method speak() that is why class Animal is an
abstract class.
27
Class Dog
28
Class Duck
29
Class Cat
30
Class Zoo with main method for Testing
Output:
31
UML Diagram for Abstract Class and Abstract Method
32
Revisited Payroll System with Abstract Class
- Use an abstract method and polymorphism to perform payroll calculations
based on the type of inheritance by an employee.
- Abstract class Employee represents the general concept of an employee.
- Subclasses: Developer, Designer, Manager.
- Abstract superclass Employee declares the “protocol” —that is, the set of
methods that a program can invoke on all Employee objects.
- We use the term “protocol” here in a general sense to refer to the various
ways programs can communicate with objects of any Employee subclass.
- Each employee has a name and a base salary defined in abstract superclass
Employee.
33
Revisited Payroll System with Abstract Class
- Class Employee provides methods earnings and toString, in addition to the get
and set methods that manipulate Employee’s instance variables.
- An earnings method applies to all employees, but each earnings calculation
depends on the employee’s class.
- An abstract method—there is not enough information to determine what
amount earnings should return.
- Each subclass overrides earnings with an appropriate implementation.
- Iterate through the array of Employees and call method earnings for each
Employee subclass object.
- Method calls processed polymorphically.
- Declaring the earnings method abstract indicates that each concrete subclass
must provide an appropriate earnings implementation and that a program will be
able to use superclass Employee variables to invoke method earnings
polymorphically for any type of Employee.
34
Interface
Abstraction on steroid
40
Interface in real life (Piano)
41
Interface
- An interface in Java is a blueprint of a class. It has static constants and
abstract methods.
- The interface in Java is a mechanism to achieve abstraction. There can be
only abstract methods in the Java interface, not method body.
- It is used to achieve abstraction and multiple inheritance in Java.
42
Interface Syntax
Declare an interface with the name
Relationship between class and
“InterfaceName” with two methods: interface:
44
Upgrade our Company Payroll with Interface
- To build an application that can determine payments for employees and invoices alike,
we first create interface Payable, which contains method getPaymentAmount() that
returns the amount that must be paid for an object of any class that implements the
interface.
- Method getPaymentAmount() is a general-purpose version of method earnings().
- Classes Invoice and Employee both represent things for which the company must be
able to calculate a payment amount. Both classes implement the Payable interface, so
a program can invoke method getPaymentAmount() on Invoice objects and Employee
objects alike.
45
UML Diagram The UML distinguishes an interface from other classes
by placing the word “interface” in guillemets (« and »)
above the interface name.
47
Recap
- Polymorphism
- Method overriding and overloading in Inheritance
- Zoo Example
- Company Payroll Example
- Abstraction
- Abstract Class
- Abstract Method
- Why we need abstract class
- Examples:
- Zoo Example
- Company Payroll Example
- Interface
- Interface in real life examples
- Upgrade Company Payroll with Invoices Example
- Abstract vs Interface
57
Thank you for your listening!
58