Interface in java

In the last tutorial, we have learned about the abstract class which is used to achieve the abstraction. By using the abstract class we can achieve 1% to 100% of abstraction. To achieve the full abstraction, the interface was introduced in java.
In this article, we will read What is an interface in Java, how to use interface, and when to use interface in Java, use of interface in java and some important java interface example .

Here is the table content of the article will we will cover this topic.
1. What is an interface in Java?
2. Properties of an interface?
3. How to create an interface?
4. How to implements an interface?
5. Difference between abstract class and interface?
6. When and how to use interface?
7. Java interface inheritance?

What is an interface in Java?

The interface is a way to achieve abstraction in JAVA. An interface can have methods and variables like class, but the methods of an interface are by default abstract. It means the interface can contain only an abstract method(Method without body). The interface is used when we want to achieve full abstraction. 

An interface is a blueprint of class and specifies what a class must do and not how to do it.
The interface has only the abstract methods, when any class implements the interface then it must provide the body of abstract methods. If a class implements an interface and doesn’t provide body to the abstract method, then the class must be declared abstract otherwise it will throw compile time exception.

Properties of an interface

1. An interface is declared with interface keyword.

2. The implements keyword is used by the class to implement an interface.

3. Before Java 8, An interface can have only abstract methods. It can’t have concrete methods (non-abstract methods). Since Java 8, an interface can have default methods and static methods. Since Java 9, an interface can have private methods.

4. All the interface methods are by default abstract and public.

5. An interface may also contain variables. All the variables are by default public, static and final.

6. An interface variable must be initialized at the time of declaration otherwise compiler will throw an error.

7. You can’t create an object of the interface.

8. An interface does not contain any constructor.

9. An interface can extend by another interface. A class can’t extend the interface. A class can implement it.

10. The implementation class has the responsibility to provide a body of all abstract methods else class should be an abstract class.

11. An interface can’t be declared as private, protected or transient.

12. A class can implement any number of interfaces by use of the comma.

13. A class cannot implement two interfaces that have methods with the same name but different return types.

 14. By the use of inheritance, we can achieve multiple inheritances which is not possible by the use of the class.

public interface MyData 
{
        // Since java 8 we can declare default method in interface
	public default void method2() 
	{
		System.out.println("method2");
	}
        // Since java 8 we can declare default method in interface
	public static void method3() 
	{
		System.out.println("method3");
	}
       	public abstract void method4();
}

How to create an interface

An interface is declared by using the interface keyword. An Interface looks like a class excepts the interface keyword. During the declaration of interface, we must take of methods because it can have only abstract methods(Method without body). But after Java 8 we can defined default method in interface. We can define variable also.

interface interfaceName
{
	// variables (by default public, static and final)
	// methods (by default public, abstract)
}
interface StudentData
{
	int rollNo = 1;
	int marks = 79;
	void showData();
	void showDetails()
}

interface: It is a keyword that is used to declare an interface.
variable: You can declare variables in interface. The variables are public, static and final by default.
abstract method: You can declare method in interface. All the methods declared in the interface are public and abstract by default.

How does compiler work after the declaration of interface?

Interface in java

How to implements an interface?

As you have read the interface is used to achieve abstraction like an abstract class. We can’t create an object of the interface. To provide the body of abstract methods, we can implement interfaces in other classes.

When a class implements an interface, it uses the interface keyword. By use of implements keyword all the variables and methods inherited in class.

Class className implements interfaceName
{
	// Body of class
}
interface Student
{
	// variable in interface
	int rollNo = 1;
	int marks = 79;
	// methods in interface
	void showData();
	void showDetails();
	
}

class Marksheet implements Student
{
	@Override
	public void showData() 
	{
		System.out.println("This is implementation of show method");
	}

	@Override
	public void showDetails() 
	{
	  System.out.println("Rollno = "+ rollNo + " and marks of student = " + marks);
	}
}

public class MainClass
{
	public static void main(String arg[])
	{
		Marksheet marksheet = new Marksheet();
		marksheet.showData();
		marksheet.showDetails();
	}
}

Output:
This is implementation of show method
Rollno = 1and marks of student = 79

2 thoughts on “Interface in java”

  1. Hi, 14. By the use of inheritance, we can achieve multiple inheritances which is not possible by the use of the class.

    Typing mistake you have done. In the above point, you have mentioned by the use of inheritance we can achieve multiple inheritances.

    It should be By the use of “Interface”

    Reply

Leave a Comment