Index

Overview


This example shows how Java interfaces can be used to solve a problem that appears unsolvable.

We'll learn the purpose of interfaces.

We need to know these first:


Basis


Initial Work


The Beginning
We started at the orange farm.

We filled baskets with oranges as we picked.

At the farm office, we performed calculations.

Sample Orange Basket Weights
BasketWeight
135
235
350
440


Example Calc
Here is an example calc.

We are simply summing the weights.
Weight
35
+35
+50
+40
Sum160

Algorithm
This is the algorithm for the fundamental calc.

A familiar loop and sum.
//Element Type: OrangeBasket
weightSum = 0
Loop through baskets
	weightSum += basket.getWeight()
Return weightSum

Report
This report shows all of our calcs.

Note they are all similar to the summing calc, so for brevity, this report will discuss only the summing calc.
Count: 4
Sum: 160
Average: 40
Min: 35
Max: 50
Standard Deviation: 6.1



Result: A Calculator


Calculator
We have coded up a calculator that:

  • Does the weight calcs
  • Generates the report

Test code has verified the calculator code (it is green).
🖩

Request for Reuse


More 'Weighable' Types
Other objects have requested to reuse our work (calculator).

Here are several class diagrams each with a type that is weighable like the orange basket.
Types That Are Weighable

Identify Specific Code


Let's figure out if our code is reusable (general).

Identify Specific Code
The calculator uses var name "baskets" and type "OrangeBasket", which are specific to the farm.
//Element Type: OrangeBasket
weightSum = 0
Loop through baskets
	weightSum += basket.getWeight()
Return weightSum



Conclusion
We did find code that is specific to our problem. Our options:
CodeAction
If mostly specificThen code (solution) is specific/unique and it will not be solve the other problems -- that happens)
If minimally specificThen we'll look for generalization opportunities

Our code is minimally specific.

Generalizing


Generalizing Var Names


Also recall generalization vs specialization.

Recall Specific Code
The var names "baskets" and type "OrangeBasket" are specific to the farm.
//Element Type: OrangeBasket
weightSum = 0
Loop through baskets
	weightSum += basket.getWeight()
Return weightSum

Generalize Variable Name
"Elements" is more general than "baskets".

Naming of vars is important (and iterative).
//Element Type: OrangeBasket
weightSum = 0
Loop through elements
	weightSum += anOrangeBasket.getWeight()
Return weightSum

Identify Remaining Specific Code
"OrangeBasket" is specific to the farm. Can it be generalized?
//Element Type: OrangeBasket
weightSum = 0
Loop through elements
	weightSum += anOrangeBasket.getWeight()
Return weightSum



Generalizing Type


Generalizing "type" is a fun challenge.

Try #1 - Type 'Object'

Try 1 - Type 'Object'
Let's try setting the type to the very general type of "Object"
//Element Type: OrangeBasket
weightSum = 0
Loop through baskets
	weightSum += basket.getWeight()
Return weightSum

Will it Work?
We're only sending one message to "element" which is getWeight. Does type "Object" understand "getWeight"?

If "Object" understood "getWeight" that means all other objects in the system would understand "getWeight" which would be odd indeed:

String name = "Foo";
name.getWeight();


Try #1 eliminated.
//Element Type: OrangeBasket
weightSum = 0
Loop through baskets
	weightSum += basket.getWeight()
Return weightSum



Try #2 - Common Superclass

Example of Common Superclass
A common superclass can be a good solution if we have closely related "sibling" classes.



Will it Work?
Unfortunately our class tree is the opposite of the simple example above.

Rather than closely related "sibling" classes, we have unrelated classes far apart in the class tree.

Try #2 eliminated.

Try Magic


Magic
Nothing has worked so far; let's try magic.
🌈
Who is the Magician?
The coder is the magician.
👸🏽
Recall Issue
Our simple (given magic) issue is that type "OrangeBasket" is specific to the farm.
//Type: OrangeBasket
weightSum += anOrangeBasket.getWeight()


Magic
With our magic wand, we make up a new type "Weighable".

And we let "weighables" understand "getWeight".
//Type: Weighable
weightSum += aWeighable.getWeight()


Java Code
And this is how we code the new type "Weighable".

It is called a Java Interface.
public interface Weighable {
	public int getWeight();
}



Test The Magic


Implementation


Will the Magic Trick Work?
Only one way to find out. Do it.
This Works
How do we know this works? Recall that when we made up type "Weighable" we stated that it understands getWeight.
//Element Type: Weighable
weightSum = 0
Loop through weighables
	weightSum += aWeighable.getWeight()
Return weightSum

But The Classes are not Weighables?
The circled classes are not type Weighable.

In fact Weighable is not even in the class tree.
Types That Are Weighable
Magically Make a Class a Weighable (Step #1)
We simply add "implements Weighable" to the class header. We now say "Suitcase" is-a "Weighable".

Note that classes can (and often will) implement multiple interfaces.
public class Suitcase implements Weighable

Magically Make a Class a Weighable (Step #2)
We'll first recall the interface "Weighable".

All methods declared in the interface must be implemented by the class.
public interface Weighable {
	public int getWeight();
}


Implement the Interface Method(s)
It is up to the class to provide the code body for the methods declared in the interface.

The "@Override" tag is optional, but helpful.
@Override
public int getWeight() {
	//Return actual weight here
	//Coder's choice
}

Any Class Can be a Weighable
We could make all the circled classes so they are Weighable's like we did for Suitcase.
Types That Are Weighable


Magical Result - A New Type


A 'Type Diagram (Tree)'
We now have a new tree with Weighable as the super-type.

We might be familar with a "Class Diagram". Actually a better term is "Type Diagram" as either interfaces/classes may be actors.

Another amazing part of this magic is that any of these types shown may belong to multiple "Type Diagrams" (implement multiple interfaces).