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

Working with interfaces in JAVA

In this article, we will learn about the concept of interfaces in JAVA. During the process, we will see how to declare interfaces how to implement them and then finally how to extend and tag interfaces.

Let us start with the basic introduction of interfaces. An interface is group of nonrepresentational methods. Interface is generally confused with class but they are not completely same. There are many things that are common between interface and class whereas there are other things which differentiate them. A class implements an interface, thereby inheriting the abstract methods of the interface. The method of writing interfaces and writing classes are completely similar to each other, but still they are entirely different topics.

Note: A class describes the attributes and behaviors of an object whereas an interface contains behaviors that a class implements and unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.

Now, let us look at various points which are similar between interface and classes. Like classes, interfaces can also contain any number of methods. Then, an interface is written in a file with a .java extension, with the name of the interface matching the name of the file. Finally, interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name. In short, these points show that interface behaves in the same way as classes do.

But they are various points which differentiate them from classes. Unlike classes, user cannot instantiate an interface. Then, interface does not contain any constructors. An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final. Finally, interfaces can extend multiple interfaces whereas classes cannot extend multiple classes.

Example of interface

Figure 1: Example of interface

Interfaces have certain properties. Firstly, interfaces are implicitly abstract. Secondly, each method in an interface is also implicitly abstract, so the abstract keyword is not needed and finally, methods in an interface are implicitly public.

Now let us declare an interface. An interface declaration consists of modifiers, the keyword interface, the interface name, a comma-separated list of parent interfaces (if any), and the interface body. Interface body is most important while declaring interfaces.

The interface body contains method declarations for all the methods included in the interface. A method declaration within an interface is followed by a semicolon, but no braces, because an interface does not provide implementations for the methods declared within it. All methods declared in an interface are implicitly public, so the public modifier can be omitted. Now, let us look at an example for declaring interfaces.

Listing 1: Shows the code for declaring an interface

/*the name which we give to our file i.e. File name : NameOfInterface.java */
import java.lang.*;
// here can be any number of import statements

public interface NameOfInterface
{
   // here can be any number of final, static fields
   // here can be any number of abstract method declarations}

In the above coding, the public access specifier indicates that the interface can be used by any class in any package. In case, if the users do not specify that the interface is public, then the interface will be accessible only to classes defined in the same package as the interface.

Now, the next step after declaring interface is to implement them. So, in order to declare a class that implements an interface, users will have to include an implements clause in the class declaration as the implements keyword is used to implement an interface. Classes created by the users can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. The implements keyword appears in the class declaration following the extended portion of the declaration. Let us learn the process of implementing the interfaces through an example.

Listing 2: Shows the code for an example

/* File name : bird.java */
interface bird {

   public void sleep();
   public void sing();
}

Listing 3: Shows the code for implementing an interface

/* File name : sparowInt.java */
public class sparowInt implements bird{

   public void sleep(){
      System.out.println("Sparow sleeps");
   }

   public void sing(){
      System.out.println("Sparow sings");
   } 

   public int noOfLegs(){
      return 0;
   }

   public static void main(String args[]){
      sparowInt n = new sparowInt();
      n.sleep();
      n.sing();
   }
} 

Listing 4: Shows the results of implementing the interfaces

Sparow sleeps
Sparow sings

There are certain points which are needed to be remembered by the user while implementing the concept of interfaces. Firstly, a class can implement more than one interface at a time. Secondly, a class can extend only one class, but implement many interfaces. Finally, an interface can extend another interface, similarly to the way that a class can extend another class.

Now, the next step after implementing the interfaces is to extend them. For this, extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface. An interface can extend another interface, similarly to the way that a class can extend another class. Let us see an example for extending the interfaces.

Listing 5: Shows the code for extending an interface

//Filename: Games.java
public interface Games
{
   public void setHostTeam(String name);
   public void setGuestTeam(String name);
}

//Filename: Cricket.java
public interface Cricket extends Games
{
   public void hostTeamScored(int points);
   public void guestTeamScored(int points);
   public void endOfInnings(int quarter);
}

//Filename: Basketball.java
public interface Basketball extends Games
{
   public void hostBasketScored();
   public void guestBasketScored();
   public void endOfPeriod(int period);
   public void overtimePeriod(int ot);
}

In the above coding, the basketball interface has four methods, but it inherits two from Games; thus, a class that implements basketball needs to implement all six methods. Similarly, a class that implements Cricket needs to define the three methods from Cricket and the two methods from Games.

Now, let us see a property which differentiates interfaces form classes. As we studied earlier, that classes can only extend one parent class and multiple inheritance is not allowed. But, interfaces are not classes, however, and an interface can extend more than one parent interface. For this, the extends keyword is used once, and the parent interfaces are declared in a comma-separated list.

Listing 6: Shows the code for extending multiple inheritance using the above example

public interface Basketball extends Games, Event

Finally, the last concept that we will be studying in this article on interfaces is tagging interfaces. As we know, 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:

Listing 7: Shows the code for tagging interfaces

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. They are as follows:

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

Finally, in this article, we have studied about the concept of interfaces in detail. In short, an interface defines a protocol of communication between two objects and an interface declaration contains signatures, but no implementations, for a set of methods, and might also contain constant definitions.

Hope you like reading it. See you next time!



I am well versed with Computer Programming languages and possess good working knowledge on software languages such as C, Java, PHP, HTML and CSS

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