How to write interfaces in java or Android?

By | August 9, 2017

This tutorial is about how to use 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.

Make easy money with infolinks

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.

However, an interface is different from a class in several ways, including:

  • You 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.

Make easy money with infolinks

Why we need Interface in Java?

An interface is a contract (or a protocol, or a common understanding) of what the classes can do. When a class implements a certain interface, it promises to provide implementation to all the abstract methods declared in the interface. Interface defines a set of common behaviors. The classes implement the interface agree to these behaviors and provide their own implementation to the behaviors. This allows you to program at the interface, instead of the actual implementation. One of the main usage of interface is provide a communication contract between two objects. If you know a class implements an interface, then you know that class contains concrete implementations of the methods declared in that interface, and you are guaranteed to be able to invoke these methods safely. In other words, two objects can communicate based on the contract defined in the interface, instead of their specific implementation.

Secondly, Java does not support multiple inheritance (whereas C++ does). Multiple inheritance permits you to derive a subclass from more than one direct superclass. This poses a problem if two direct superclasses have conflicting implementations. (Which one to follow in the subclass?). However, multiple inheritance does have its place. Java does this by permitting you to “implements” more than one interfaces (but you can only “extends” from a single superclass). Since interfaces contain only abstract methods without actual implementation, no conflict can arise among the multiple interfaces. (Interface can hold constants but is not recommended. If a subclass implements two interfaces with conflicting constants, the compiler will flag out a compilation error.)

An interface allows to represent an agreement between classes on how they will talk to each other without being tied to the actual implementations.This allows us to replace implementations by others (very useful for testing, or changing use cases) without changing the compiled code.

Sample Code

Here I will show you how to do it in Android or Java.
This example is for Android.

So can we start.

This is the interface that I am going to implement.

Interface Class

package com.coderzheaven.interfacetest1;

public interface MyTestInterface {

	void testFunctionOne();

	void testFunctionTwo();

}

Now we will write the activity that implements the above interface.

package com.coderzheaven.interfacetest1;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity implements MyTestInterface {

	@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");
	}
}

Ok Done. Go on and Run it.

7 thoughts on “How to write interfaces in java or Android?

  1. Krishh

    ur code is flawless .But interface methods are not executing.

    Can you please provide code of how we can transfer data between two activities using interface .

    Reply
  2. Rakesh R

    Useful tutorial..
    I suggest to beginners to use Toast instead of SOP…

    Reply
  3. Thomas

    This tutorial isn’t very good. You’ve only explained what to do, but not why or how you’re doing it this way. Given this, someone new would not be able to transfer this knowledge into their own situation.

    Reply
    1. James Post author

      Thomas, Thanks for the comment. I have added explanation to those too. Please check the updated post.

      Reply
  4. priya

    Interface is a keyword interface name can be user defined name the default signature of variable is public static final and for method is public abstract.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *