View RSS Feed

My Java Tips

  1. Interface extending Interface

    by , 11-13-2011 at 12:42 PM (My Java Tips)
    An interface can extend other interface but cannot implement any interface. This makes sense because interface cannot have any implementation. An interface can only contain abstract methods that are implemented by the class implementing that interface.

    Lets do this with an example.

    Create a package named myinterfaces. Using conventions, package name should be in small case. Now create an interface named InterfaceA with 2 methods.

    Java Code:
    package myinterfaces;
  2. Implementing ActionListener interface

    by , 11-12-2011 at 06:54 PM (My Java Tips)
    ActionListener interface is used to perform actions on a performed event. In this post, I will write about its importance and usage.

    ActionListener interface is part of java.awt.event package. It has only one abstract method that has to be implemented in the class implementing this interface.

    Method signature:
    void actionPerformed(ActionEvent e)

    Consider the following scenario: you developed an GUI form for an application comprising of text fields, ...
  3. Using Interfaces

    by , 11-10-2011 at 05:32 PM (My Java Tips)
    Interfaces are used to define a contract for the classes. It has its own importance when it comes to defining standards.

    Consider the following example:

    Cars have some standard and their basic operations are the same. Each car should have operations like startCar, moveCar, stopCar etc. Toyota, Mazda and Honda are cars and each has its own way of starting, moving and stoping. So each has to have its own startCar, moveCar and stopCar method.

    How to enforce ...
  4. Working With Interfaces

    by , 11-08-2011 at 07:33 PM (My Java Tips)
    Interface is very much similar to the Abstract class in Java but the difference is that in interfaces, members (methods) cannot be implemented, member(fields) defined will be treated as constants.
    A class becomes more formal about its behavior after implementing an interface. Interfaces are actually a contract between the class and the outside world. While implementing an interface in a concrete class, you have to implement all the methods defined by that interface. If you miss any method, ...
    Java SE
  5. Factory Methods

    by , 06-02-2011 at 08:11 PM (My Java Tips)
    This post lists some facts about factory methods.

    Factory methods are static methods that return an instance of the native class. Some examples from JDK include :

    • LogManager.getLogManager
    • Pattern.compile
    • Collections.unmodifiableCollection
    • Collections.synchronizeCollection
    • Calendar.getInstance

    Factory methods do not have names like constructors and do not need to create a new object upon each invocation. Objects can be cached and reused, if ...
  6. Using interfaces in APIs

    by , 05-25-2011 at 10:59 AM (My Java Tips)
    When you are designing your API, you might want to use interface since this seems a common practice. There exist a style in Java programming where everything is expressed in terms of Java interfaces instead of classes.

    Using interfaces provides benefits but its not a good idea for an entire API to be expressed in terms of them.

    Use interface as a type if you have a good reason for it to be. Following are some important facts to be considered:

    Interfaces ...
    Tags: api, interface Add / Edit Tags
    Java SE