interface definitions and abstract Classes

Related slides on offer

Figure 577. Interface examples Slide presentation

Figure 578. Observations Slide presentation

Multiple standards involved:

8P8C

Mechanical dimensions and tolerances.

CAT7

Telecommunication performance of twisted-pair copper interconnects.

Note: Compatible hardware must obey both standards.


Figure 579. Writing strings to file Slide presentation
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();}
}

Figure 580. Using Text2File Slide presentation
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

Figure 581. Possible Text2File errors: Slide presentation
  • 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.


Figure 582. Employ “try-with-resources” Slide presentation
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

Figure 583. interface syntax Slide presentation
accessModifier interface interfaceName [throwsClause]?{
    [field]*
   [method]*
}

Figure 584. The AutoCloseable promise Slide presentation
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; ❺
  }
}

❶

Java™ standard package.

❷

Method declaration without implementing body.

❶

Promise to implement the AutoCloseable interface. This boils down to implement public void close() at ❸.

Notice the @Override annotation resemblance to overriding base class methods in derived classes.

❷

final has been removed in favour of setting out to null at ❺.

❸

Renaming former closeFile() method to close() keeping the interface promise made at ❶.

Caution: No @Override here since close() is being implemented rather tan overriding a base class method not existing anyway.

❹

Closing the PrintStream thereby flushing buffered strings prior to releasing allocated operating system resources.

❺

Setting out to null causes subsequent Text2File.println(...) calls throwing a NullPointerException. Thus silent errors become observable errors.


Figure 585. abstract class replacement Slide presentation
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;
  }
}

❶

“Private” project package.

❷

Replacing interface by abstract class.

❸

abstract close() not providing an implementing body.

❶

Extending abstract class AutoCloseable replaces implementing the AutoCloseable interface.

❷

Overriding (and in fact implementing) the abstract base class close() method.


Figure 586. interface vs. abstract class Slide presentation
  • Java™ disallows multiple inheritance.

  • A class may implement an arbitrary number of interfaces:

    public class X implements I1, I2, I3 {...}

Figure 587. interface MyAutoCloseable Slide presentation
/**
 * Support auto-closing of resources
 */
public interface MyAutoCloseable {
  /**
   * close resource in question. Example: Terminate
   * a database connection or a file stream.
   */
  public void close();
}

Figure 588. Extending MyAutoCloseable to flush Slide presentation
/**
 * Flush pending values.
 */
public interface MyFlushable extends MyAutoCloseable {

  /**
   * Save pending i.e. buffered values.
   */
  public void flush();
}

Figure 589. Using MyFlushable Slide presentation
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;
  }
}

Figure 590. Inheritance hierarchy Slide presentation

Figure 591. Upcoming topics Slide presentation
  • Default methods.

  • Base classes.