Thursday, November 19, 2009

Key Java Concepts I - Interfaces

    This is a part of a series of key important Java concepts to remember. Much of the fundamental points are based from a variety of books, forums, websites etc. The objective is to make these fundas handy at any moment. This post will cover the concept of Interfaces.
    •  Interfaces are like 100-percent abstract classes.
    • Interfaces methods are by default public and abstract - however the explicit declaration is optional.  
    • Interfaces can be implemented by any class.
    • Interfaces can have constants.
    • Interface constants are implicitly public, static and final.
    • An interface can have only abstract methods - no concrete methods are allowed.
    • Interfaces cannot extend a class or implement any other interface.
    • However, Interfaces can extend as many interfaces as possible.
    • As you know, there is no multiple inheritance allowed in Java i.e a class cannot extend more than one class BUT a class can implement multiple interfaces.
    • The class implementing an interface can itself be abstract.
    • An abstract implementing class need not have to implement the interface methods, however the non-abstract (concrete) class MUST implement the methods if it implements an interface.
    Illustration:

    public abstract Sizeable{  // use of public modifier is optional, if you don't want default access
        public abstract  toDo();  // Note the abstract methods always end with a ; instead of {}
        public abstract doThis();
    }

    class Baloon implements Sizeable {}

    public class Tyre mplements Sizeable {}


      Stumble Upon Toolbar

      No comments:

      Post a Comment

      Related Posts with Thumbnails