Java Interface

Java Interface

Java Interface

Java Interface is something similar to a class which is a collection of some abstract methods.

All the methods in an interface are abstract methods

Interface can have some other things like constants, different kinds of methods, and nested types.

Where do we use Java Interface

Well if we need to pass data or pass some instructions to somewhere else(Some other class) in that case we can use interface

We can achieve data abstraction, multiple inheritances

If you know some other use then please comment below. keeping that in mind let’s move on how it looks.

Facts

Interface looks like a class but it is a bit different from the class like

  1. We cannot instantiate an interface
  2. It does not contain constructors
  3. The methods in an interface are abstract
  4. We cannot use it with extend, we have to implement it in a class
  5. We can implement as much interface we want

Example One: How to declare Java Interface

public interface OnCarListener {
    // any number of final, static fiels
    // the methods has to be abstract method
    void drive();
    void stop();
}

We have to use the interface keyword to declare it. Then we give a name to the interface. We can put some abstract methods or final or static fields.

In here OnCarListener is the interface

How to use Interface

Now our java interface has created, one way we can use it like

package com.example.androvaiddemoapp;

public class Car implements OnCarListener{

    public static void main(String args[]) {
        Car mCar = new Car();
        mCar.drive();
        mCar.stop();
    }

    @Override
    public void drive() {
        System.out.println("Car is Moving");
    }

    @Override
    public void stop() {
        System.out.println("Car has stopped moving");
    }
}

Here we can see that we have implemented the interface in our car class. So we can now override the two abstract methods from the interface and work with those methods. Run it one IDE and you will see the output like

Car is Moving
Car has stopped moving

Passing Data with Interface

The most use of interface that I have found when I want to pass some data to another class instantaneously I can use interface. There are some other approaches that we can use but today we will be discussing interface data pass.

public interface OnCarListener {
    // any number of final, static fiels
    // the methods has to be abstract method
    void drive(String drivingData);
    void stop();
}
public class Car implements OnCarListener{

    public static void main(String args[]) {
        Car mCar = new Car();
        mCar.drive("This car has started and it is moving");
        mCar.stop();
    }

    @Override
    public void drive(String drivingData) {
        System.out.println(drivingData);
    }

    @Override
    public void stop() {
        System.out.println("Car has stopped moving");
    }
}

Example Two:

In android development

To Pass data from an Adapter to any Activity or in any fragment we can uses Interface

We will see an example related to this data pass. We can find another example in android.  Please visit the Android Category.

package com.androvaid.javainterface;

import android.os.AsyncTask;

public class SearchTask extends AsyncTask<Void, Void, Void> {

    public interface OnSearchListener {
        void onSearchStarted();
        void onSearchFinished(String message);
    }
    
    private OnSearchListener onSearchListener;

    public SearchTask(OnSearchListener listener) {
        this.onSearchListener = listener;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (onSearchListener != null) {
            onSearchListener.onSearchStarted();
        }
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Do something 
    }

    @Override
    protected void onPostExecute() {
        super.onPostExecute();
        if (onSearchListener != null) {
            onSearchListener.onSearchFinished("Search has complited successfully");
        }
    }
}
package com.androvaid.javainterface;

import com.androvaid.javainterface.SearchTask;

public class CheckFingerprintActivity implements SearchTask.OnSearchListener{
    
    public static void main(String args[]) {
      SearchTask searchTask = new SearchTask(this);
      searchTask.execute();
   }

    @Override
    public void onSearchStarted() {
        System.out.println("Searching...");
    }

    @Override
    public void onSearchFinished(String message) {
        System.out.println(message);
    }

}

 

So we now have some ideas about the interface and how it works and how we can use it in our project. For more, you can see java doc or you can see Wikipedia.

If you have anything to know or ask please leave a comment below so that we can discuss it. Thank you for connecting with androvaid.com.

Leave a Reply