Tuesday, December 6, 2016

Java Interface and Exception.

Understanding Interfaces and Abstract Methods
  • To define what a class must do but not how to do it until later in specific application.
    • An abstract class define its signature and return types, namely, its interface for multiple processes or methods without implementations.
  • How? Specify methods with no body ({}).
  • Actions are defined in a class that implements the interface.
  • One interface, multiple methods is the essence of polymorphism.
  • Prior to JDK 8, interfaces could not define any implementation whatsoever. JDK 8 changes the rule so that default implementation to an interface method is allowed.
  • The original intent behind interface/abstract class remains. and default implementation in essence is a special use feature.
  • JDK 8 also added the ability to define static methods in interfaces.
    • Same as static methods in a class, static methods in interfaces can be called independently of any object created from it.
    • Yes, no implementation or instance of the interface is required to call a static method.
    • To call, specify the interface name, a period and the method name.
    • Static interface methods are not inherited by implementing class or a sub-interface extended from the parent interface.

Using Interface References
  • You can create an interface reference variable and use it to refer to any object that implements its interface. 
  • When you call a method on an object through an interface reference, you are calling the version of the method implemented by the object that is executed at runtime. 
  • This is similar as using a superclass reference to access a child class's methods on its objects. 

Using Interface to Share Constants
  • Though controversial, one usage of interface is to share constants among multiple classes.
  • Variables in interfaces are implicitly public, static and final and these are the characteristics of constants.
  • Examples of applications includes array size, various limits, special values and the like.

Extending Interface
  • An interface can inherit another interface by extending. 
  • When a class implements the derived interface, it must implement all methods in the inheritance chain.
interface X {
   void method1 ();
   void method2 ();
}

interface Y extends X {
   void method3 ();
}

class Z implements Y {
   public void method1 () {
      // do something...
   }
   public void method2 () {
      // do something...
   }
   public void method3 () {
      // do something...
   }
}

class Z2 implements Y {
   // implements all three methods
}

// Using Interface References
class Demo {
   public static void main (String args[]) {
   Z  obz  = new Z();
   Z2 obz2 = new Z2();
   X  obx  = new X();
   obx = obz;
   System.out.println ("Class z attribute is " + obx.getAttribute());
   obx = obz2;
   System.out.println ("Class z2 attribute is " + obx.getAttribute());
}


Exceptions
  • All exceptions are represented by classes.
  • All exceptions are derived from "Throwable" class.
  • When an exception occurs, an object of some exception class is generated.
  • Two direct subclasses of Throwable: Exception and Error.
    • Class Error exceptions are those occur in JVM, not in the program.
    • Class Error exceptions are usually beyond programmer's control.
    • Class Exception exceptions are those from program activity, such as divide-by-zero, array out-of-boundary and file-not-found errors. These are called standard exceptions.
    • Class Exception exceptions should be handled in the program.
      • RuntimeException is an important subclass of Exception class and is used to represent various common types of runt-time errors.
  • Another type of exceptions are those thrown manually by using throw statement.

Exceptions Handling
  • try
    • If an exception occurs within the try block, it is thrown.
  • catch
    • If an exception is thrown by try block, it will be handled here in a predictable way.
  • throw
    • Use throw to manually throw an exception.
  • throws
    • Use throws clause in the declaration of a method to specify an exception could be thrown out of a method.
  • finally
    • Use finally block to specify any activity that must be executed upon exiting from a try block.


No comments:

Post a Comment