Chapter 6 - Poplymorphism
Chapter 6 - Poplymorphism
Chapter 6 - Poplymorphism
Polymorphism
By
En. Mohd Nizam bin Osman
(Senior Lecturer)
Department of Computer Science
Faculty of Computer and Mathematical Science, UiTM
Perlis
Objectives
• By the end of this chapter, students should be able to:
POLYMORPHISM 3
Introduction
• Polymorphism refers to the ability to associate many meanings
to one method name by a special mechanism known as late
binding (dynamic binding).
4
Introduction
• Polymorphism refers to many forms. It comes from Greek word "poly"
(means many) and "morphos" (means form).
• There are many forms of the same methods to describe different actions.
7
Constructing Polymorphism
(Abstract Class)
• Abstract class – is the class that has the common attributes and
methods of several classes.
8
Constructing Polymorphism
(Abstract Class)
• All other classes (concrete class) will extend from this
abstract class and provide the implementation of the
methods’ body (methods’ definition).
9
Constructing Polymorphism
(Abstract Method)
• An abstract method is a method with only signature (i.e., the
method name, the list of arguments and the return type) without
implementation (i.e., the method’s body).
abstract
public abstract double calculatePerimeter();
abstract
public abstract String toString();
10
Constructing Polymorphism
• In Java, once you have the abstract method in a class, the entire class must
also be declared as an abstract class.
• Example:
public abstract class AbstractClassExample
{
public abstract double calculateArea();
public abstract double calculatePerimeter();
public abstract String toString();
}
11
Constructing Polymorphism
• A class containing one or more abstract methods is called an
abstract class.
12
Constructing Polymorphism
14
Constructing Polymorphism
• If a class extends a class with an abstract
method and does not implement that abstract
method, then that method remains abstract in
the subclass. Consequently, the subclass is
also an abstract class and must be declared
abstract.
15
Constructing Polymorphism
(Example)
Calculate Area &
Triangle Circle
Perimeter
2D
What do you have to do?
Shape We need to list out
all attributes and methods
for each class.
Rectangle
16
Constructing Polymorphism
(Example)
Class: Rectangle Class: Triangle Class: Circle
Attributes: Attributes: Attributes:
length width (base) radius
width height area
area area perimeter
perimeter perimeter
17
Constructing Polymorphism
(Example)
ST
• Determine the common attributes and methods between classes
E
P
1 • Use polymorphism concept - Create a general class that will have only the
ST common attributes and methods/abstract method (without method body)
( abstract class)
E • Abstract methods refer to the method that will be used for all classes (specific
P class), but with different implementation such as the formula involved.
2
ST • Then we extend all the shape classes (specific class) from the general class
by using the inheritance concepts. (concrete class)
E • Write the methods’ definition for abstract method for each specific class.
P
3 18
Constructing Polymorphism
(Abstract and Concrete Class
Example 1)
//To declare abstract object TwoD in a file named TwoD.java
//Put class TwoD in a package
package twoD;
//Constructor
public TwoD()
{
area=0;
perimeter=0;
}
19
Constructing Polymorphism
(Abstract and Concrete Class
Example 1)
//Getter or accessor methods
public double getArea(){return area;}
//Processor methods
public abstract void calculateArea();
20
Constructing Polymorphism
(Abstract and Concrete Class
Example 1)
// To declare concrete object Rectangle in a file named
// Rectangle.java
//Put class Rectangle in a package
package twoD;
//constructor
public Rectangle()
{
super();
width=0;
length=0;
}
21
Constructing Polymorphism
(Abstract and Concrete Class
Example 1)
//setter or mutator methods
public void setWidth(double w)
{
width=w;
}
//processor methods
public void calculateArea()
{
area=length*width;
}
22
Constructing Polymorphism
(Abstract and Concrete Class
Example 1)
public void calculatePerimeter()
{
perimeter=2*length+2*width;
}
//printer methods
public void display()
{
System.out.println("RECTANGLE:");
System.out.println("The width : " +width);
System.out.println("The length : " +length);
System.out.println("The area : " +area)";
System.out.println("The perimeter: " +perimeter);
}
}
23
Constructing Polymorphism
(Abstract and Concrete Class
Example 1)
//To declare concrete object Circle in a file named Circle.java
//put class circle in a package
package twoD;
//constructor
public Circle()
{
super();
radius=0;
}
//printer methods
public void display()
{
//Formating
DecimalFormat dF= new DecimalFormat("0.00");
System.out.println("CIRCLE: ");
System.out.println( "The radius : " +radius);
System.out.println("The area : " +dF.format(area));
System.out.println("The perimeter : "+dF.format(perimeter));
}
}
25
Constructing Polymorphism
(Abstract and Concrete Class
Example 1)
//To declare object TestingTwoD.java in a file named TestingTwoD.java
import twoD.TwoD;
import twoD.Rectangle;
import twoD.Circle;
import java.util.*;
26
Constructing Polymorphism
(Abstract and Concrete Class
Example 1)
for(int i=0; i<5; i++)
{
//display menu 1-rectangle 2-circle
System.out.print(("Your choice?(1/2)");
int c = scanner.nextInt();
if(c==1)
{
//input data
System.out.print("Input the width :");
double width = scanner.nextDouble();
System.out.print("Input the length :");
double len = scanner.nextDouble();
27
Constructing Polymorphism
(Abstract and Concrete Class
Example 1)
Rectangle rec = new Rectangle();
//Setting the data into an object
rec.setWidth(width);
rec.setLength(len);
//calculation
rec.calculateArea();
rec.calculatePerimeter();
else{
//input number as string
System.out.print("Input the radius :");
double radius = scanner.nextDouble();
• 31
The method display() – polymorphism method because it refers to
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
//class privateInstitution - base case / general case
public abstract class privateInstitution
{
private String institutionName;
private char typeService;
private int noOfWorkers;
private int noOfCustomers;
//Default constructor
public privateInstitution()
{
institutionName = “”;
typeService = ‘ ’;
noOfWorkers = 0;
noOfCustomer = 0;
}
32
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
//Normal constructor
public privateInstitution(String instName,char typService,
int worker, int customer)
{
institutionName = instName;
typeService = typService;
noOfWorkers = worker;
noOfCustomer = customer;
}
//Mutator
public void setInstitutionName(String instName)
{ institutionName = instName; }
public char setTypeService(char typService)
{ typeService = typService; }
public void setNoOfWorkers(int worker)
{ noOfWorkers = worker; }
public int setNoOfCustomers()
{ noOfCustomers = customer; }
33
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
//Accessor
public String getInstitutionName()
{ return institutionName; }
//abstract methods
public abstract double calcProfit();
public abstract String toString();
}
34
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
//class privateClinic - sub class/concrete class
public class privateClinic extends privateInstitution
{
final int Fewer_Rate = 15;
final int External_Rate = 120;
final int Diabetes_Rate = 52;
final int Blood_Rate = 45;
private String nameDoc;
private int time;
//Default constructor
public privateClinic()
{
super();
nameDoc = “”;
time = 0;
}
35
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
//Normal constructor
public privateClinic(String instName,char typService,
int worker, int customer, String nmDoc, int tm)
{
super(instName, typService, worker, customer);
nameDoc = nmDoc;
time = tm;
}
//Mutator
public void setNameDoctor(String nmDoc)
{ nameDoc = nmDoc; }
36
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
//Accessor
public String getNameDoctor()
{
return nameDoc;
}
37
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
//abstract method
public double calcProfit()
{
double value = 0.0;
switch(getTypeService())
{
case 'F': value = Fewer_Rate * getNoOfCustomers();
break;
39
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
//class privateCollege - sub class/concrete class
public class privateCollege extends privateInstitution
{
final int Computer_Rate = 2500;
final int Business_Rate = 2200;
final int Accounting_Rate = 2300;
final int Pra_Rate = 2700;
private String nameLec;
//Default constructor
public privateCollege()
{
super();
nameLec = “”;
}
40
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
//Normal constructor
public privateCollege(String instName, char typService,
int worker, int customer, String nmLec)
{
super(instName, typService, worker, customer);
nameLec = nmLec;
}
//Mutator
public void setNameLecturer(String nmLec)
{ nameLec = nmLec; }
//Accessor
public String getNameLecturer()
{ return nameLec; }
41
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
//Abstract method
public double calcProfit()
{
double value = 0.0;
switch(getTypeService())
{
case '1': value = Computer_Rate * getNoOfCustomers();
break;
case '2': value = Business_Rate * getNoOfCustomers();
break;
case '3': value = Acounting_Rate * getNoOfCustomers();
break;
case '4': value = Pra_Rate * getNoOfCustomers();
break;
}
return value;
}
42
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
//Printer
public String toString()
{
return “\nInstitutionName: "+getInstitutinName()+"\n"+
"Type Service:"+getTypeService()+"\n"+
"No.Customer:"+getNoOfCustomers()+"\n"+
"No. Workers:"+getNoOfWorkers()+"\n"+
“Lecturer Name:"+nameLec;
}
}
43
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
//Application program
public class privateInstitutionApp
{
public static void main(String[] args)
{
Scanner scanner = new Scanner (System.in);
String lineSeparator = System.getProperty(
"line.separator");
scanner.useDelimiter(lineSeparator);
44
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
for(int j = 0; j<pi.length; j++)
{
System.out.print("Institution Name: ");
String instName= scanner.nextLine();
System.out.print("Service Type:");
String type = scanner.nextLine();
char typService = type.toUpperCase().charAt(0);
System.out.print("Number of workers: ");
int worker = scanner.nextInt()
System.out.print("Number of customer: ");
int customer = scanner.nextInt();
45
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
if(privateType == 'C')
{
System.out.print("Doctor Name: ");
String nameDoc= scanner.nextLine();
System.out.print("Number of workers: ");
int tm = scanner.nextInt();
47
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
//the total of profit for diabetes treatment
double diaProfit = 0.0;
48
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
//the average profit fees for all colleges
int noCollege = 0;
double allClgProfit = 0.0;
49
Constructing Polymorphism
(Abstract and Concrete Class
Example 2)
//Print information of clinic make profit >=5000
for(int m = 0; m<pi.length; m++)
{
if(pi[m] instanceof privateClinic)
{
if(pi[m].calProfit() >= 5000)
System.out.println(+pi[m].toString());
}
}
52
The End
Q&A
53