Programming‎ > ‎Java‎ > ‎

4. Interfaces and Inner Classes







Interfaces

  • Interface is a way of describing what classes should do, without specifying how they should do it
  • A class can implement one or more interfaces
  • All methods of an interface are automatically public, so not required to specify access modifier
  • Interfaces can also define constants, fields are always public static final
  • Interfaces never have instance fields
  • Interfaces could have method definition from Java8 as default definition
  • Some interfaces define just constants and no methods
  • You can declare Interface variables, refer to object of class that implements interface

public interface Comparable {
  int compareTo(Object other);
}

// Any class that implements Comparable interface is required to have compareTo() method with same signature

class Employee implements Comparable {

   public int compareTo(Object other) {
      Employee e = (Employee) other;
      if (salary < other.salary) return -1;
      if (salary > other.salary) return 1;
      return 0;
   }
}

If (e instanceof Comparable) {...}

java.lang.Comparable
  • int compareTo(Object otherObject) - compares this object with otherObject and returns -ive, zero, +ive integer
java.util.Arrays
  • static void sort(Object[] a) - sort the elements in the array a, using tunes mergesort, all element if array must belong to object of class that implements Comparable interface

A class implements the interface by 2 steps
  • Declare the class implements the interface
  • Supply definition of all methods in the interface along with access modfier (e.g. public)

  • Class must implement compareTo() method to avail itself if the sorting service
  • But why can's Employee implement compareTo() without implementing Comparable interface?
  • The reason for interfaces is that Java language is strongly types
  • When making a method call, the compiler needs to able to check that the method actually exist
  • If object variable is type Comparable, then existence of the method is assured
Interfaces and Abstract Classes
  • Why can't Comparable simply be an abstract class
  • A generic property can't be expressed using the class inheritance, since multiple class inheritance is not supported

Object Cloning

  • A clone of an object is a new object that has the same state as the original but a different identity
  • If you class implements Cloneable, the clone method in the Object class will make an exact copy of your class's object
  • The clone method is a protected method of Object, which means that your code cannot simply call it.
  • Only the Employee class can clone Employee objects
  • The default cloning operation is "shallow"
  • If the subobjects that is shared between the original and the shallow clone is immutable, then the sharing is safe
  • Even if the default (shallow copy) is adequate, you still need to implement the Cloneable interface, redefine clone to be public, call super.clone(), and catch the CloneNotSupportedException
Employee copy = (Employee) original.clone);

class Person implements Cloneable {
   public Object clone() { 
      try {
         return super.clone();
      }
      catch(CloneNotSupportedException e) { return null;}

   }
}


Inner Classes

  • Inner classes are technically somewhat complex, they are defined inside other classes
  • Their methods can access the fields of the surrounding class











Comments