• interface definitions and abstract Classes
Image layer 1
Image layer 2
Image layer 3
Image layer 4
Image layer 5
Image layer 6

Multiple standards involved:

8P8C

Mechanical dimensions and tolerances.

CAT7

Telecommunication performance of twisted-pair copper interconnects.

Note: Compatible hardware must obey both standards.

public class Text2File {
  private final PrintStream out;

  public Text2File(final String fileName)
     throws FileNotFoundException {
    out = new PrintStream(new File(fileName));}

  public void println(final String s) {
    out.println(s);}

  public void closeFile() {
    out.close();}
}
final String outputFileName =
            "output.txt";
try {
  final Text2File output =
  new Text2File(outputFileName);
  output.println("Some dumb text");
  output.println("More dumb text");
  output.closeFile();
} catch (final FileNotFoundException e){
  System.err.println("Unable to open '"
   + outputFileName + "' for writing");
}

File output.txt:

Some dumb text
More dumb text
  • Missing output.closeFile() call.

    Some text portion may not be flushed to disk.

  • Calling output.println(...) after output.closeFile():

    output.closeFile();
    output.println("Too late!");

    Last call will be silently ignored.

final String outputFileName =
     "output.txt";

try (final Text2File output =
     new Text2File(outputFileName)){
  output.println("Some dumb text");
  output.println("More dumb text");
} catch (FileNotFoundException e){...}

Compile time error:

Required:
   java.lang.AutoCloseable
Found:
  de.hdm_stuttgart.mi.sd1.Text2File
accessModifier interface interfaceName [throwsClause]?{
    [field]*
   [method]*
}
package java.lang; 

public interface AutoCloseable {

/**
 * Closes this resource,
 * relinquishing any
 * underlying resources.
 */
void close​() ;

}
public class Text2File
  implements AutoCloseable {

  private  PrintStream out;
...
  public void println(final String s){
    out.println(s); }

  public void close() {
    out.close(); 
    out = null; 
  }
}
package hdm.project; 

abstract public
    class AutoCloseable {

/**
 * Closes this resource,
 * relinquishing any
 * underlying resources.
 */
abstract void close​();

}
public class Text2File
  extends AutoCloseable {

  private  PrintStream out;
...
  public void println(final String s){
    out.println(s); }

  @Override public void close() {
    out.close();
    out = null;
  }
}
  • Java disallows multiple inheritance.

  • A class may implement an arbitrary number of interfaces:

    public class X implements I1, I2, I3 {...}
/**
 * Support auto-closing of resources
 */
public interface MyAutoCloseable {
  /**
   * close resource in question. Example: Terminate
   * a database connection or a file stream.
   */
  public void close();
}
/**
 * Flush pending values.
 */
public interface MyFlushable extends MyAutoCloseable {

  /**
   * Save pending i.e. buffered values.
   */
  public void flush();
}
public class Text2FileFlushable implements MyFlushable {
  private PrintStream out;
...
  /**
   * Flushing pending output to underlying file.
   */
  public void flush(){
    out.flush();
  }
  /**
   * Closing file thereby flushing buffer. Caution: Further calls
   * to {@link #println(String)} will fail!.
   */
  public void close() {
    out.close();
    out = null;
  }
}
Image layer 1
Image layer 2
Image layer 3
  • Default methods.

  • Base classes.

  • interface definitions and abstract Classes
    • ➟ Interfaces and sorting
Image layer 1
Image layer 2
Image layer 3
Image layer 4
interface Comparable<T> {
                ┌────┘  
                
  int compareTo​(T o);

}
public class String implements Comparable <String >, ... {
  ...                           ┌────────────┘          
  @Override                     
  public int compareTo(final String  other) {
     ...
    return ...;
  }
}
System.out.println("Eve".compareTo("Paul"));      
System.out.println("Victor".compareTo("Andrew")); 
System.out.println("Hannah".compareTo("Hannah")); 
-11 
21  
0   
Image layer 1
Image layer 2
Image layer 3
Image layer 4
Image layer 5
Image layer 6
Image layer 7
Image layer 8
Image layer 9
Image layer 10
Image layer 11
Image layer 12
  1. Antisymmetric: sgn(x.compareTo(y)) == -sgn(y.compareTo(x))

  2. Transitive: x.compareTo(y) > 0 and y.compareTo(z) > 0x.compareTo(z) > 0.

  3. x.compareTo(y)==0 ⇒ that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.

  4. Recommendation: (x.compareTo(y)==0) == (x.equals(y))

final String[] names = { 
  "Laura", "Aaron", "Tim", "Peter", "Eve", "Bernie"
};

Arrays.sort(names); 

for (final String n: names) { 
  System.out.println(n);
}
Aaron
Bernie
Eve
Laura
Peter
Tim
  1. Understanding Arrays.sort()
  2. Sorting Rectangle instances by width
  3. Sorting Rectangle instances by width and height
Unsorted Case sensitive Case insensitive Descending
UK
quick
hello
sign
ATM
ATM
UK
hello
quick
sign
ATM
hello
quick
sign
UK
sign
quick
hello
UK
ATM

Solution: Provide your own Comparator!

import java.util.Comparator;

public class SortCaseInsensitive implements Comparator<String> {
                              ┏━━━━━━━━━━━━━━┳━━━━━━━━━━━┛
  @Override                   ▼              ▼
  public int compare(final String a, final String b) {
    return a.toLowerCase().compareTo(b.toLowerCase());
  }
}
System.out.println("hello".compareTo("UK")); 

System.out.println(new SortCaseInsensitive(). 
   compare("hello", "UK"));
19 
-13 
final String[] names = {
"UK", "quick", "hello", "sign", "ATM"
};

Arrays.sort(names, new SortCaseInsensitive());

for (final String n: names) { 
  System.out.println(n);
}
ATM
hello
quick
sign
UK
final String[] names = {
"UK", "quick", "hello", "sign", "ATM"
};
Arrays.sort(names, (a, b) -> b.compareTo(a)); 

for (final String n: names) { 
  System.out.println(n);
}
sign
quick
hello
UK
ATM
  1. Adding flexibility in sorting rectangles
  2. A nonsense generator
  3. An interface based plotter
  4. Various integer array algorithms
  5. A command line version computing a sample's average and median
  6. Adding line numbers to text files
  7. A partial implementation of GNU UNIX wc