Docsity

Understanding Dynamic Binding and Measurable Interface in Java, Study notes of Computer Science

Toggle sidebar
bg1
1
1
CS162: Introduction to
Computer Science II
Polymorphism
2
Polymorphism
• A reference can be polymorphic, which can
be defined as "having many forms"
obj.doIt();
• This line of code might execute different
methods at different times if we change the
object that obj points to
3
Polymorphism
• Polymorphic references are resolved at run
time
– this is called dynamic binding or late binding
• Careful use of polymorphic references can
lead to elegant, robust software designs
• Polymorphism can be accomplished using
inheritance or using interfaces
4
Polymorphism via Inheritance
• It is the type of the object being
referenced that determines which
method is invoked
•Example:
Object a;
a = new Circle( 2.0 );
double x = a.area();
a = new Square( 2.0 );
double y = a.area();
5
Polymorphism via Interfaces
• An interface name can be used as the
type of an object reference variable
Doable obj;
• The obj reference can be used to point to
any object of any class that implements
the Doable interface
• The version of doThis (invoked by the
following line) depends on the type of
object that obj is referencing
obj.doThis();
6
Interfaces and polymorphism
•Interface
– Contract specifying expected behavior.
– Specifies operations that must be implemented,
and specifies their return types / parameter lists.
– Does not provide implementation.
•Polymorphism
– For developing re-usable code.
– Use same code for different data types.
– Use same code to invoke different (type-
dependent) tasks.
pf2

Partial preview of the text

Download Understanding Dynamic Binding and Measurable Interface in Java and more Study notes Computer Science in PDF only on Docsity!

1

CS162: Introduction to

Computer Science II

Polymorphism

2

Polymorphism

• A reference can be polymorphic , which can

be defined as "having many forms"

obj.doIt();

• This line of code might execute different

methods at different times if we change the

object that obj points to

3

Polymorphism

• Polymorphic references are resolved at run

time

– this is called dynamic binding or late binding

• Careful use of polymorphic references can

lead to elegant, robust software designs

• Polymorphism can be accomplished using

inheritance or using interfaces

4

Polymorphism via Inheritance

• It is the type of the object being

referenced that determines which

method is invoked

• Example:

Object a;

a = new Circle( 2.0 );

double x = a.area();

a = new Square( 2.0 );

double y = a.area();

5

Polymorphism via Interfaces

• An interface name can be used as the

type of an object reference variable

Doable obj;

• The obj reference can be used to point to

any object of any class that implements

the Doable interface

• The version of doThis (invoked by the

following line) depends on the type of

object that obj is referencing

obj.doThis();

6

Interfaces and polymorphism

• Interface

– Contract specifying expected behavior.

– Specifies operations that must be implemented,

and specifies their return types / parameter lists.

– Does not provide implementation.

• Polymorphism

– For developing re-usable code.

– Use same code for different data types.

– Use same code to invoke different (type-

dependent) tasks.

7

A Measurable interface

public interface Measurable {

/* interface requiring a single numeric

measure to be applied to any realizing class

double getMeasure();

boolean greaterThan(Measurable x);

8

Example: Debts

public class Debt implements Measurable { private double balance, payment, intRate;

public Debt (double bal, double pay, double rate) { //etc. }

public boolean greaterThan (Measurable rhs) { Debt x = (Debt) rhs; // type cast return balance > x.balance; }

public double getMeasure() { return balance; }

// toString, etc. }

Required by the Measurable interface

9

Example: Shapes

public class Shape implements Measurable { private String shapeName;

public Shape() { shapeName = ”undefined”; } // etc.

public boolean greaterThan (Measurable rhs) { Shape x = (Shape) rhs; // type cast return area() > x.area(); // dynamic binding

public double getMeasure() { return area(); } }

Required by the Measurable interface

10

Example: Generics

public class GenericList { private Measurable[] myData; private int count; public GenericList(int size) { count = 0; myData = new Measureable[size]; }

public void addSorted(Measurable d) { int k = 0; while ((k < count) && d.greaterThan(myData[k])) k++; for (int j = count; j > k; j--) myData[j] = myData[j-1]; myData[k] = d; count++; } // etc. }

Dynamic binding

Type is interface name

Array of Measurable references, not Measurable instantiation

11

Using the generic list …

// other declarations ... GenericList debtList = new GenericList(10); GenericList shapeList = new GenericList(10);

debtList.addSorted(new Debt(5000.00, 327.51, 0.055)); debtList.addSorted(new Debt(2000.00, 200.00, 0.06)); // etc. ...

shapeList.addSorted(new Circle(3.0)); shapeList.addSorted(new Square(7.1)); // etc. ...

12

Type compatibility

• What happens if we try to add a Shape to

debtList?

debtList.addSorted(new Circle(3.0));

• In GenericList …

while ((k < count) && d.greaterThan(myData[k]))

d is a Shape, and myData [ k ] is a Debt

• In Shape …

public boolean greaterThan (Measurable rhs) { Shape x = (Shape) rhs; // type cast

Attempt to cast Debt to Shape will fail.