interface definitions and abstract Classes

Related slides on offer

Figure 546. Interface examples Slide presentation

Figure 547. 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 548. 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 549. 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 550. 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 551. 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 552. interface syntax Slide presentation
accessModifier interface interfaceName [throwsClause]?{
    [field]*
   [method]*
}

Figure 553. 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 554. 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 555. 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 556. 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 557. Extending MyAutoCloseable to flush Slide presentation
/**
 * Flush pending values.
 */
public interface MyFlushable extends MyAutoCloseable {

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

Figure 558. 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 559. Inheritance hierarchy Slide presentation

Figure 560. Upcoming topics Slide presentation
  • Default methods.

  • Base classes.