Polymorphism EEE321.09 - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Polymorphism EEE321.09

Description:

The difference between education and training. Imagine your 13 year old niece/nephew/sister/brother... comes home from school and tells ... – PowerPoint PPT presentation

Number of Views:136
Avg rating:3.0/5.0
Slides: 19
Provided by: Kni69
Category:

less

Transcript and Presenter's Notes

Title: Polymorphism EEE321.09


1
Polymorphism EEE321.09
Royal Military College of Canada Electrical and
Computer Engineering
  • Maj JW Paul
  • Jeff.Paul_at_rmc.ca
  • 1-613-541-6000 x6656

2
The difference between education and training
Imagine your 13 year old niece/nephew/sister/broth
ercomes home from school and tellsyou they are
taking sex training
3
Lab 3 Review
4
Design decisions should be part of your discussion
public Box (double x1, double y1, double x2,
double y2) testCoords(x1,y1,x2,y2) counter
ID counter this.x x1 this.y
y1 this.ur_x x2 this.ur_y y2 public
Box () this(0.0, 0.0, 1.0, 1.0) private
void testCoords (double xTest, double yTest,
double ur_xTest, double ur_yTest)
if(yTestgtur_yTest xTestgtur_xTest)
throw new IllegalBoxException(this.toString())
5
Code Reuse
public void resize(double x2, double
y2) testCoords(this.x, this.y, x1, y2) ur_x
ur_xNew ur_y ur_yNew
public void resize(double factor) testCoords(0.0
, 0.0, factor, 1.0) ur_x ur_xNew ur_y
ur_yNew
Recall testCoords if( y gt ur_y x gt ur_x )
throw ...
6
Access to private attributes
class myClass private int x private int
y public myClass (A a) this.x
a.x this.y a.y
//somewhere in main myClass A new
myClass() myClass B new myClass(A) ... if (
A.x B.x ) ...
7
More comments
What does this do? List myList new
ArrayList()
What does this mean? System.out.println(myBox.ID
)
Q are all constructors public?
8
What defines a Class/Object?
Proper terminology
  • A name
  • Something it knows
  • Something it does

properties attributes state
operations methods
9
Recall
Name
StudentData
s00001StudentData
studentName studentNumber studentPhone
studentName String studentNumber
int studentPhone String
Attributes
what it knows
listenInClass() study() passExams()
listenInClass() Boolean study()
Boolean passExams() double
Methods
what it does
10
Todays Class
  • Polymorphism

11
What is polymorphism?
  • appearing in many forms

parametric
universal
sub-type inclusion
polymorphism
overloading
ad-hoc
coercion
12
Overloading
  • A single identifier denotes several abstractions
  • 2 2
  • 2.0 2.0
  • Also applies to methods
  • resize(double x, double y)
  • resize(double factor)

13
Coercion
  • A single abstraction serves several types through
    implicit type conversion
  • 2 2.0
  • 2.0 2

14
Inclusion
  • An abstraction operates through an inclusion
    relation

Many Java programmers refer to sub-type
polymorphism as polymorphism
15
Parametric
  • An abstraction operates uniformly across
    different types
  • a function or datatype can be written generically
    so that it can deal equally well with objects of
    various types
  • aka generics (to be introduced in Java 5)

16
Parametric example
  • a function append that joins two lists
  • does not depend on one particular type of list
  • can append lists of integers, lists of real
    numbers, lists of strings, and so on.
  • Let a denote the type of elements in the lists.
  • Then append can be typed a  a ? a, where
    a denotes a list of elements of type a.
  • We say that append is parameterized by a.
    (Note that since there is only one type
    parameter, the function cannot be applied to just
    any pair of lists they must consist of the same
    type of elements.)

http//en.wikipedia.org/wiki/Polymorphism_(compute
r_science)
17
Advantage of Universal Polymorphism
  • reduces code bloat
  • both can be mimicked with overloading
  • but that means you must code it all

18
Review
  • What is the purpose of patterns
  • How do we use them?
  • A pattern is NOT a flowcharted solution
  • A pattern needs context to be a solution
Write a Comment
User Comments (0)
About PowerShow.com