Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How Interfaces are implemented in Java

We will learn how we can implement interfaces in java, talking about the multiple inheritances of interfaces as well.

You use interfaces to define a protocol of behaviour that can be implemented by any class anywhere in the class hierarchy. Thus a better approach to the spreadsheet cell problem would be to create an interface, let’s say CellAble, that is meant to defined the list of methods that a "cell value" must implement: toString(), draw(), toFloat(), and so on. Then the spreadsheet cells could contain any type of object that implemented the CellAble interface (or in other words implemented the list of methods). Objects that implemented the CellAble interface would not have to be hierarchically related. Let us see below how can interfaces be useful.

Interfaces are useful for:

  • capturing similarities between unrelated classes without forcing a class relationship
  • declaring methods that one or more classes are expected to implement
  • revealing an object's programming interface without revealing its class (objects such as these are called anonymous objects and can be useful when shipping a package of classes to other developers)

A class declares all of the interfaces that it implements in its class declaration. To declare that your class implements one or more interfaces, use the keyword implements followed by a comma-delimited list of the interfaces implemented by your class.

For example, consider the Collection interface introduced on the previous page. Now, suppose that you were writing a class that implemented a FIFO queue (a First-In-First-Out queue). Because a FIFO queue object contains other objects it makes sense for the class to implement the Collection interface. The FIFOQueue class would declare that it implements the Collection interface like this:

Listing1: Introducing the FIFO class

class FIFOQueue implements Collection {
    . . .
    void add(Object obj) {
        . . .
    }
    void delete(Object obj) {
        . . .
    }
    Object find(Object obj) {
        . . .
    }
    int currentCount() {
        . . .
    }
}

There by guaranteeing that it provides implementations for the add(), delete(), find(), and current Count() methods.

By convention, the implements clause follows the extend clause if there is one.

Note that the method signatures of the Collection interface methods implemented in the FIFO Queue class must match the method signatures as declared in the Collection interface.

Implementing an Interface

You may implement as many interfaces in a class as you wish; just separate them with commas. For example:

Listing2: Implementing an Interface

// Note: 
//   ActionListener requires defining actionPerformed(...)
//   MouseMotionListener requires defining mouseMoved(...) and mouseDragged(...).

public class MyPanel extends JPanel implements ActionListener, MouseMotionListener {
    public void actionPerformed(ActionEvent e) {
        /* Method body */
    }
    public void mouseDragged(MouseEvent me) {
        /* Method body */
    }
    public void mouseMoved(MouseEvent me) {
        /* Method body */
    }
    // Everything else in this class.
}

It is common for a panel that does graphics and responds to the mouse to implement its own mouse listeners (but not action listeners) as above.

The given below code illustrates that interface in java support multiple inheritance In order to implement interface we implement a keyword implement. The Tutorial describes how to create and use interface method in your class.

The step involved in the program are described below:

  1. We have taken an interface check ,that implement a empty method message ( ).
  2. Inside the class we create an object of interface check and implement a body to the method message.
  3. The object t call and expose a message ( ) method to the outside world.
  4. In case there is an exception in the try block, the subsequent catch block handle the exception.

Listing3: Code displaying the multiple inheritance

Interface.java
interface check {
public void message();
}
public class Interface {
public static void main(String[] args) {
  try {
  check t = new check() {
  public void message() {
  System.out.println("Method defined in the interface");
  }
  };
  t.message();
  } catch (Exception ex) {
  System.out.println("" + ex.getMessage());
  }
  }
  }

Extending Multiple Interfaces:

A Java class can only extend one parent class. Multiple inheritances are not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface.

The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.

For example, if the Hockey interface extended both Sports and Event, it would be declared as:

public interface Hockey extends Sports, Event

Tagging Interfaces:

The most common use of extending interfaces occurs when the parent interface does not contain any methods. For example, the MouseListener interface in the java.awt.event package extended java.util.EventListener, which is defined as:

package java.util;
public interface EventListener
{}

An interface with no methods in it is referred to as a tagging interface. There are two basic design purposes of tagging interfaces:

Creates a common parent:

As with the EventListener interface, which is extended by dozens of other interfaces in the Java API, you can use a tagging interface to create a common parent among a group of interfaces. For example, when an interface extends EventListener, the JVM knows that this particular interface is going to be used in an event delegation scenario.

Adds a data type to a class:

This situation is where the term tagging comes from. A class that implements a tagging interface does not need to define any methods (since the interface does not have any), but the class becomes an interface type through polymorphism.

Conclusion

We learnt how we can implement interfaces in java, talking about the multiple inheritances of interfaces as well.



I am a software developer from India with hands on experience on java, html for over 5 years.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login