Skip to main content

How to write interfaces in java or Android?

what is an Interface?
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements.
Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.

MainActivity.java
public class MainActivity extends Activity implements Interface {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public void testFunctionOne() {
        System.out.println("Print from testFunctionOne in the Interface");
    }
    @Override
    public void testFunctionTwo() {
        System.out.println("Print from testFunctionTwo in the Interface");
    }}
in Interface.java
public interface Interface  {    
    void testFunctionOne();
    void testFunctionTwo(); }

Comments