|
|
|
|
DEMO: 14-interfaces/10-abstract-class/Demo.java (show compile error)
|
DEMO: demo/14-interfaces/10-abstract-class/Demo2.java + Circle.java
Previously: we have written the selectionSort() method to sort an array of Circles:
public static void selectionSort(Circle[] list)
{
for (int i = 0; i < list.length-1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
Circle min = list[i]; // Assume first element is min
int minIndex = i; // Index where min is found
for ( int k = minIndex+1; k < list.length; k++ )
if ( list[k].getArea() < min.getArea() ) // Sort Circles based by area
{
min = list[k]; // Update min value
minIndex = k; // Update its index
}
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
Circle help = list[minIndex]; // Standard exchange alg
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
Then we made the selectionSort() method more general by using a superclass data type:
public static void selectionSort(GeometricObject[] list)
{
for (int i = 0; i < list.length-1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
GeometricObject min = list[i]; // Assume first element is min
int minIndex = i; // Index where min is found
for ( int k = minIndex+1; k < list.length; k++ )
if ( list[k].getArea() < min.getArea() ) // Sort objects based by area
{
min = list[k]; // Update min value
minIndex = k; // Update its index
}
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
GeometricObject help = list[minIndex]; // Standard exchange alg
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
$64,000 question: will this solution work if GeometricObject is an abstract class ???
Yes: because we do not need to instantiate any GeometricObject in the program (no new operation used !)
public static void selectionSort(GeometricObject[] list) // Works without any changes
{
for (int i = 0; i < list.length-1; i++)
{
/* -----------------------------------------------
Find the minimum in the list[i..list.length-1]
----------------------------------------------- */
GeometricObject min = list[i]; // Assume first element is min
int minIndex = i; // Index where min is found
for ( int k = minIndex+1; k < list.length; k++ )
if ( list[k].getArea() < min.getArea() ) // Sort objects based by area
{
min = list[k]; // Update min value
minIndex = k; // Update its index
}
/* ------------------------------------------------------
Swap list[i] with list[minIndex] if necessary
------------------------------------------------------ */
if ( minIndex != i )
{ // Swap list[minIndex] and list[i]
GeometricObject help = list[minIndex]; // Standard exchange alg
list[minIndex] = list[i];
list[i] = help;
}
}
}
|
DEMO: demo/14-interfaces/11-abstract-class/Demo.java + Circle.java + GeometricObject.java
|
|
DEMO: demo/14-interfaces/12-abstract-method
|
|
DEMO: demo/14-interfaces/13-final-abstract-class/MyClass.java
|
DEMO: demo/14-interfaces/13-final-abstract-method/MyClass.java
|