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

Java Interfaces: Overview and Implementation

In this article we will learn about the java interfaces and their implementation and use in java programming.

Java programming language already have the basic building block of a program i.e. Class. So why do we need to have an interface? We will discuss this later in this article but let see what is an interface first.

An interface is a core part of java programming language and it is a feature to achieve abstraction in java. Inside an interface there can be only abstract methods and static constants. In general, an interface is used to achieve multiple inheritance in java with full abstraction concept, since multiple inheritance in java is not allowed. An interface is also known as blueprint of a class.

Class and interfaces are two different concepts, where a class describes the attributes and behaviours of an object, and an interface contains behaviours that a class implements.

Advantages of using Interface:

Below are some reasons to use an interface.

  • Interface is used to achieve abstraction.
  • Interface is the only way to achieve multiple inheritance in java.
  • Most important advantage of using an interface is that it can be used to achieve loose coupling that is very helpful in larger applications.

Features of Interface

  • An interface also represents IS-A relationship.
  • An interface cannot be instantiated.
  • An interface in java is declared using keyword ‘interface’ and this keyword represents a type like we have ‘class’ keyword.
  • An interface can’t be instantiated in java, rather a reference variable of type interface can be created that reference variable can point to any implementation of that interface in Java. It’s also a good OOPS principle to "program for interfaces than implementation"
  • All variables declared inside an interface is implicitly public final variables or constants. We can use both interfaces and classes to store application level constants but here using an interface have some advantage that we can implement interface and can directly access constants without referring them with a class name.
  • All methods declared inside Java Interfaces are implicitly public and abstract, even if the public or abstract keywords are not use. We cannot define any concrete methods in an interface.
  • In Java it’s legal for an interface to extend multiple interfaces.
  • Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.

An interface is similar to a class in the following ways:

  • An interface can contain any number of methods.
  • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
  • The bytecode of an interface appears in a .class file.
  • Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

An interface is different from a class in several ways:

  • We cannot instantiate an interface.
  • An interface does not contain any constructors.
  • All of the methods in an interface are abstract.
  • An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
  • An interface is not extended by a class; it is implemented by a class.
  • An interface can extend multiple interfaces.

Declaration of an Interface

The interface keyword is used to declare an interface. Below is a simple example of declaring an interface:

Example - Let us look at an example that declares an interface named as MyInterface:

    /* File name: MyInterface.java */
  import java.lang.*;
   
  public interface MyInterface
  {
                  //Any number of final, static fields
                   //Any number of abstract method declarations
  }

Interfaces have the following properties:

  • An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.
  • Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
  • Methods in an interface are implicitly public.

Implementing Interfaces:

When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.

A class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.

Listing 1: Declaration and implementation of interface.

  interface  MyInterface
  {
      public void myInterfaceMethod1(); 
      //Not using abstract keyword as it is default
      void myInterfaceMethod12(); 
      //Not using public keyword as it is default
  }
   
  /* DemoInterface class – Start */
  public class DemoInterface implements MyInterface
  {
                    //Defining the interface methods
                     public void myInterfaceMethod1 (){
        System.out.println("Interface Method 1");
     }
   
     public void myInterfaceMethod12(){
        System.out.println("Interface Method 2");
     } 
   
    public static void main(String args[]){
        DemoInterface  obj = new DemoInterface ();
        obj. myInterfaceMethod1 ();
        obj. myInterfaceMethod12 ();
     }
  }
  /* DemoInterface class – End */

Output Listing 1:

Interface Method 1
Interface Method 2

Explanation Listing 1: The above example shows the way to declare an interface and how to implement interface in our class. Here we simply created an interface.You can notice one thing we did’t use the keyword abstract in interface method declaration, however public keyword can also be omitted from the method declaration. Next we created a class implementing the interface and in this class we defined the functions of interface which we implemented.

When we override methods of an interface in implemented class we must follow certain rules:

  • Checked exceptions should not be declared on implementation methods other than the ones declared by the interface method or subclasses of those declared by the interface method.
  • The signature of the interface method and the same return type or subtype should be maintained when overriding the methods.
  • An implementation class itself can be abstract, and if so, interface methods need not be implemented.
  • A class can implement more than one interface at a time.
  • A class can extend only one class, but implement many interfaces.
  • An interface can extend another interface, similarly to the way that a class can extend another class. When an interface extends another interface, the keyword which we use is extends.

Interfaces and Inheritance

An interface can extend another interface, similarly to the way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface

  • The new interface then adds fields and methods to the existing definition of interface.
  • A class that implements the new interface must implement all methods from the base interface as well as all the additional methods from the new interface definition.
  • An interface can actually extend multiple base interfaces, in which case the combination of all the methods will be required for any implementing class.

The following Listing shows how to extend interfaces .

Listing 2: Extending interfaces and then implementing the interface in class.

 
  interface  MyInterface
  {
      public void myInterfaceMethod1(); 
     //Not using abstract keyword as it is default
      public void myInterfaceMethod2();
  }
   
  interface  MyInterface1 extends MyInterface
  {
    public void myInterfaceMethod3(); 
    // Public keyword can be removed, as it is default
    public void myInterfaceMethod4();
  }
   
   
  public class DemoInterface implements MyInterface1
  {
      public void myInterfaceMethod1 (){
        System.out.println("Interface Method 1");
     }
   
     public void myInterfaceMethod2(){
        System.out.println("Interface Method 2");
     } 
   
    public void myInterfaceMethod3 (){
        System.out.println("Interface Method 1");
     }
   
     public void myInterfaceMethod4(){
        System.out.println("Interface Method 2");
     } 
   
   
    public static void main(String args[]){
        DemoInterface  obj = new DemoInterface ();
        obj. myInterfaceMethod1 ();
        obj. myInterfaceMethod2();
        obj. myInterfaceMethod3();
        obj. myInterfaceMethod4();
     }
  }

Output Listing 2:

Interface Method 1
Interface Method 2
Interface Method 3
Interface Method 4

Explanation Listing 2: The above example shows how we can create multiple interfaces and use interface inheritance. As you can notice in our class we implement only one interface i.e. MyInterface1. And this interface is extending another interface MyInterface. Ultimately MyInterface1 interface will have the four method declarations, and in theclass implementation we would require to give definitions for all the four methods.

Conclusion:

In this tutorial we learned the basic concept of interfaces in java, and got the idea how to create interfaces, and implement interfaces in classes. Finally, we looked into a practical sample of using multiple interfaces in a simple program.



Computer system engineer who has 4 year of experience in the field our web-development ,FPGA, and java programming.

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