An interface in Java programming is a type of reference that, like a class, can consist of constants, method declarations, default methods, static methods, and nested types. It outlines a contract for classes that are required to implement it, setting forth the methods that must be implemented without specifying how they should be implemented.
Here is the tutorial for Kotlin
Interface Example:
1/ Create A New Project and Crate an Array then add some value in array.
package com.tutorialb.interfacetest;
import android.os.Bundle;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> bookList= new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
bookList.add("Java Book");
bookList.add("Kotlin Book");
bookList.add("Flutter Book");
}
}
2/ Create a Interface Name is BookData
package com.tutorialb.interfacetest;
public interface BookData {
void myBook(String bookName);
}
3/ Create new Class name is BookClass
package com.tutorialb.interfacetest;
public class BookClass {
}
4/ Create a Constructor and pass the value by parameter and create A Function getMyBOOK
package com.tutorialb.interfacetest;
import java.util.ArrayList;
public class BookClass {
ArrayList<String> bookList;
BookData bookData;
public BookClass( ArrayList<String> bookList , BookData bookData){
this.bookList=bookList;
this.bookData=bookData;
}
public void getMyBook(int index){
String book= bookList.get(index);
bookData.myBook(book);
}
}
5/ Now Implements Interface Like BookData and Override myBook
package com.tutorialb.interfacetest;
import android.os.Bundle;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements BookData{//implement Here
ArrayList<String> bookList= new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
bookList.add("Java Book");
bookList.add("Kotlin Book");
bookList.add("Flutter Book");
}
@Override
public void myBook(String bookName) {// Override Here
}
}
6/ Finally Now Call Constructor by parameter then call getMyBook Function
package com.tutorialb.interfacetest;
import android.os.Bundle;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements BookData{
ArrayList<String> bookList= new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
bookList.add("Java Book");
bookList.add("Kotlin Book");
bookList.add("Flutter Book");
BookClass bookClass=new BookClass(bookList,MainActivity.this); //Call Constructor
bookClass.getMyBook(1); //Call Function
}
@Override
public void myBook(String bookName) {
Toast.makeText(this, bookName, Toast.LENGTH_SHORT).show(); //Output Here
}
}