java by aviatwebz1


									              Java Interfaces and packages
                            Java Interfaces
          Java allows us to take this one stage further, by formally recording the
                                  interface as a Java interface
         a java interface is just a collection of abstract methods (i.e. we state the
                                signatures, but not the bodies)

states that                                                             note no
this is an         public interface MyStack {                           bodies for
interface,            public int size();                                the methods
not a class
                      public boolean isEmpty();
                      public Object top();
                      public void push(Object elt);
                      public Object pop();
                   }                                                                    1
          How should we design classes?

1.   obtain a statement of the problem

2.   sketch a sample scenario
3.   work out what objects are involved

     (do the following one class at a time:)
4.   work out how those objects are meant to behave

5.   design the interface for the class        the interface only shows the public
                                               methods and data
                                                 ● it does not show private data or
6.   define the variables
                                                 ● it does not state how the class is
7.   implement the methods                          implemented

8.   test the class
                  interfaces vs classes
– a class definition can contain instance/class variables and instance/class
  methods, including both the signature and the body

– a java interface contains only the signatures of public instance methods
  (and named constants)

– a java interface acts like a specification
    • it says what it means to be e.g. a stack

    • to be a stack, an object must offer at least those methods in its public



Attraction                     Auxiliaries                    Demonstration

Movie                          Symphony

 • How do we organize above classes into a single unit? Put them in file?
   However, only one public class per file (whose name is same as file’s)
 • Solution: Place several files (compilation units) into a package

units of organizing related Classes, Interfaces, Sub packages
     Reduce name clashing
     Limit visibility of names

Java programs typically organized in terms of packages and subpackages
    Each package may then be divided into several packages, subpackages, and
    Each class can then be stored in a separate file

Each source file starts with something like:
             package mypackage;

    Code in source file is now part of mypackage


To top