Skip to content
Related Articles

Related Articles

Improve Article
How to Create Interfaces in Android Studio?
  • Last Updated : 18 Feb, 2021

Interfaces are a collection of constants, methods(abstract, static, and default), and nested types. All the methods of the interface need to be defined in the class. The interface is like a Class. The interface keyword is used to declare an interface.

public interface AdapterCallBackListener {

   void onRowClick(String searchText);

}

public interface OnFragmentInteractionListener {



   void onFragmentInteraction();

}

So basically in android, there are two types of interfaces we can create and we use frequently

  • Creating Java Interface
  • Creating Kotlin Interface

So in this article, we are going to create both Java and Kotlin Interface in Android studio.

Creating Java Interface in Android Studio 

Like a class, a Interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).  

  • Interfaces specify what a class must do and not how. It is the blueprint of the class.
  • An Interface is about capabilities like a Player may be an interface and any class implementing Player must be able to (or must implement) move(). So it specifies a set of methods that the class has to implement.
  • If a class implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstract.
  • A Java library example is Comparator Interface. If a class implements this interface, then it can be used to sort a collection.

Syntax:

interface <interface_name> {

   // declare constant fields



   // declare methods that abstract  

   // by default.

}

To declare an interface, use the interface keyword. It is used to provide total abstraction. That means all the methods in an interface are declared with an empty body and are public and all fields are public, static, and final by default. A class that implements an interface must implement all the methods declared in the interface. To implement interface use implements keyword.

Step by Step Implementation

Step 1: Go to Android Studio and open the project in Android mode as shown in the below image.

Step 2: Now go to the app > java > your package name > right-click > New > Java Class as shown in the below image.

Step 3: After completing step 2 a pop-up screen will arise like below. Here enter your interface name and choose the Interface and click the Enter button.



After completing the above steps successfully you can find your Java interface here. Go to the app > java > your package name > GeeksforGeeks.java. And you can write your own Java code here.

Creating Kotlin Interface in Android Studio 

Interfaces are custom types provided by Kotlin that cannot be instantiated directly. Instead, these define a form of behavior that the implementing types have to follow. With the interface, you can define a set of properties and methods, that the concrete types must follow and implement. The interface definition in Kotlin begins with the interface keyword followed by the name of the interface, followed by the curly braces within which the members of the interface reside. The difference is that the members will have no definition of their own. These definitions will be provided by the conforming types.

Example:

interface Vehicle()

{

    fun start()

    fun stop()

}

Step by Step Implementation

Step 1: Go to Android Studio and open the project in Android mode as shown in the below image.



Step 2: Now go to the app > java > your package name > right-click > New > Kotlin File/Class as shown in the below image.

Step 3: After completing step 2 a pop-up screen will arise like below. Here enter your class name and choose the Interface and click the Enter button.

After completing the above steps successfully you can find your Kotlin class here. Go to the app > java > your package name > GeeksforGeeks.kt. And you can write your own Kotlin code here.

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!



My Personal Notes arrow_drop_up
Recommended Articles
Page :