Professional Documents
Culture Documents
Object Oriented
Programming Techniques
Interface Techniques
Objective
Interface
related programming
techniques
Interface - Introduction
Interface
declares a behaviour
implementing interfaces
Multiplicity
Method implementation
all methods should be implemented
Interfaces
extending interfaces
Multiplicity
Subtypes (revisited)
defines a type
Reference types in Java
Interface
class type,
array type or
interface type
The
Subtypes (revisited)
Assume:
Interfaces and
polymorphic classes
Interfaces
StudentProcessing
Student
EmployeeProcessing
StudentEmployee
Employee
is subtype of both
StudentProcessing and
EmployeeProcessing
Instances
of StudentEmployee can
be viewed as students or employees
10
of StudentEmployee seen
as student
11
12
topic in OO language
design
StudentEmployee class
implements two interfaces
inherits no implementation
Consequence
getAvgMarks()
distinct code in Student and
StudentEmployee classes
getSalary()
distinct code in Employee and
StudentEmployee classes
sources of inconsistency
Ioan Salomie - Programming Techniques
13
14
PersonProcessing
StudentProcessing
EmployeeProcessing
StudentEmployee
15
16
problem
Question
How many 'name' a StudentEmployee object has?
Questions
Which copy is accessed?
Can we access both copies?
17
reuse in Java
18
StudentProcessing
EmployeeProcessing
StudentImpl
Student
EmployeeImpl
StudentEmployee
Employee
19
20
21
using delegation
through delegation
Consistency is preserved
22
A class may
extend one class
implement multiple interfaces
An interface may
extend multiple interfaces
Methods with
resolve rules
different signatures => overloaded
same signature && same return type =>
same method
same signature && different return types =>
compile error
same signature && same return type but
throw different exceptions => same method,
union of throw lists
Ioan Salomie - Programming Techniques
23
24
public interface X {
static final int c = ;
}
public interface X {
static final double c = ;
}
public class C implements X, Y {
void aMethod() {
X.c // the int constant
Y.c // the double constant
}
}
two separate constants c (one int, one double)
Ioan Salomie - Programming Techniques
25
Marker interfaces
public interface M {}
public class C implements M { }
M
is an empty interface
M and C - subtype relationship
Class C is marked as having a certain
property
The property is given (highlighted) by the
name of the interface
Example
of such interface
Clonable
It distinguishes the classes that can be cloned
of those that cannot be cloned
26
Classes implementing
multiple interfaces
Flat approach
interface GeometricCalculations {
double calcArea();
double calcPerimeter();
}
public class Circle implements
GeometricCalculations, Comparable, Serializable {
//
public int compareTo(Object o) { }
public double calcArea() { }
public double calcPerimeter() { }
}
27
Classes implementing
multiple interfaces
Hierarchical approach
28
Mixin
Modify
existing classes to
implement certain interfaces
additional facilities to its basic
functionality
Example: Serializable, Comparable,
Cloning
29
feature
differences
30
Abstract classes
Methods
Instances
Constructors
A partial implementation
Interfaces
Public methods
Constants
No implementation allowed
Features
An interface
Interface inheritance
Allow for multiple interface extensions
A class can implement multiple interfaces
A class
Can only extend ("implementation inheritance") one
other class.
Ioan Salomie - Programming Techniques
31
criteria
system evolution
able or can do vs. is-a
homogeneity vs. heterogeneity
speed
third party functionality
32
33
class approach
34
approach
public interface IF {
public void m();
public void m1();
}
All
Conclusions
35
Abstract
class
36
class
37
Slow
Requires extra indirection to identify
the corresponding method in the actual
class
Modern JVMs new ways to reduce
this speed penalty
Abstract
class
Fast
38
class
39
interface List {
int size();
boolean isEmpty();
// other methods
}
abstract class AbstractList implements List {
public abstract int size();
public boolean isEmpty() {
return size() == 0;
}
// other class elements
}
class ArrayList extends AbstractList {
public int size() { }
// other class elements
}
Ioan Salomie - Programming Techniques
40
Abstract
Collection
<<interface>>
List
AbstractList
ArrayList
AbstractSequential List
Vector
LinkedList
<<interface>>
Serializable
<<interface>>
Clonable
41
0 - Top hierarchy
42
of programming in terms of
interfaces
Much of the implementation is already
done in superclasses
Easy switch between implementations
Develop parallel implementations
43