CPSC 2150 - DAY 8 SEPTEMBER 13, 2016 ================================================================================ Classes are not enough to solve the information hiding problem. Users are still burdened because they have to look at all the class details A class is a particular solution to a problem .it has a specific private data representation .it has methods based on private data representation In general, there are many solutions with different trade-offs for the same problem An interface is a place to specify a contract and it provides a seperation of concerns between class users and implementers In general, there is no one best solution to a problem Some more space conscious and some more time conscious A subset of methods faster in one; a different subset faster in another In real-time systems, it is good to allocate large size arrays, rather than dynamically allocate memory. An interface describes what classes should do. Does not describe how they should do it. EX. public interface Salaried { void setSalary(BigDecimal d); BigDecimal getSalary(); } to satisfy this interface, a class must provide setSalary and getSalary methods Interface Declaration --------------------- Keyword interface replaces class Methods have no body No constructiors Like a class, an interface can contain .Fields .Must be public static final (ie constants) .these qualifiers are usually ommitted Instantiating an interface Interface Salaried { ... } Salaried payee; //ok But interfaces CANNOT be instantiated directly payee = new Salaried(); //WRONG! Only classes can be instantiated directly Variable of type I can refer to an instance of class that implemnts I Dynamic type is set at run time (by new) Compiler can not infer dynamic type Operator instanceof tests the run-time type.