interface definitions and abstract Classes

Related slides on offer

Figure 500. Interface examples Slide presentation Create comment in forum

Figure 501. Observations Slide presentation Create comment in forum

Multiple standards involved:

8P8C

Mechanical dimensions and tolerances.

CAT7

Telecommunication performance of twisted-pair copper interconnects.

Note: Compatible hardware must obey both standards.


Figure 502. Writing strings to file Slide presentation Create comment in forum
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 503. Using Text2File Slide presentation Create comment in forum
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 504. Possible Text2File errors: Slide presentation Create comment in forum
  • 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 505. Employ try-with-resources Slide presentation Create comment in forum
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 506. interface syntax Slide presentation Create comment in forum
accessModifier interface interfaceName [throwsClause]?{
    [field]*
   [method]*
}

Figure 507. The AutoCloseable promise Slide presentation Create comment in forum
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 508. abstract class replacement Slide presentation Create comment in forum
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 509. interface vs. abstract class Slide presentation Create comment in forum
  • Java disallows multiple inheritance.

  • A class may implement an arbitrary number of interfaces:

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

Figure 510. interface MyAutoCloseable Slide presentation Create comment in forum
/**
 * 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 511. Extending MyAutoCloseable to flush Slide presentation Create comment in forum
/**
 * Flush pending values.
 */
public interface MyFlushable extends MyAutoCloseable {

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

Figure 512. Using MyFlushable Slide presentation Create comment in forum
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 513. Inheritance hierarchy Slide presentation Create comment in forum

Figure 514. Upcoming topics Slide presentation Create comment in forum
  • Default methods.

  • Base classes.