Java Interfaces - The Coding Shala

Home >> Learn Java >> Java Interfaces

Java Interfaces

Java Interfaces are used to achieve Abstraction. Interfaces are like abstract class but the interface only contains an abstract method. Every method declared in an interface are by default abstract. We can see the interface as the blueprint of the class. It specifies what a class must do and not how.
Java Interface - The Coding Shala
We use the interface keyword to declare interfaces. To implement the interface we use implements keyword. 

The following code shows how to use java interfaces: 

interface interface_name{
 //declare constant fields
 //abstract methods
 //by default
}

class class_name implements interface_name{
 //class body
 //implementation of all the abstract methods
}

The following are the important point about Java interfaces: 
  • Java Interfaces provide total abstraction.
  • In java interfaces all the methods are abstract and all fields are public, static and final by default. 
  • If a class implements an interface then the class must implement all the methods declared in the interface.
  • Using interfaces we can achieve multiple inheritances.
  • Java Interface also represents the IS-A relationship.
  • Java Interface cannot be instantiated. we can not create the object of interfaces.
  • After Java 8, Interfaces can have default and static methods also.
  • After Java 9, Interfaces can have private methods also.
  • An interface can extend another interface.

Example of Java Interface

The following example explains Java Interface: 


//Java Interface example
//thecodingshala.com

interface intfc1{
 public static final int val1 = 10; //public static final
 
 abstract void className();
 void msg(); //this is also abstract by default 
}

class Class1 implements intfc1{
 @Override
 public void className() {
  System.out.println("Class1 implements intfc1 interface");
 }
 @Override
 public void msg() {
  System.out.println("All methods are abstract in interface by default");
  System.out.println("value of field is: "+val1);
 }
}

class Main{
 public static void main(String[] args) {
  Class1 c = new Class1();
  c.className();
  c.msg();
 }
}
Output: 

Class1 implements intfc1 interface
All methods are abstract in interface by default
value of field is: 10

Java Interface can have default implementation means a default method with a method body. We don't need to implement the default method in the class. Java interfaces can have static methods also which can be called independently without an object. The following example explains it: 

//Java Interface example
//thecodingshala.com

interface intfc1{
 default void Display() {
  System.out.println("This is interface intfc1");
 }
 
 static void Msg() {
  System.out.println("Static method");
 }
}

class Class1 implements intfc1{
 
}

class Main{
 public static void main(String[] args) {
  Class1 c = new Class1();
  c.Display();
  intfc1.Msg(); //static method can be called independently 
 }
}
Output: 

This is interface intfc1
Static method

Java Interface Inheritance

An interface can also extend another interface. The following example explains it: 
//Java Interface example
//thecodingshala.com

interface intfc1{
 void className();
}

interface intfc2 extends intfc1{
 void msg();
}
class Class1 implements intfc2{
 public void className() {
  System.out.println("Inside Class1");
 }
 public void msg() {
  System.out.println("Hello There");
 }
}

class Main{
 public static void main(String[] args) {
  Class1 c = new Class1();
  c.className();
  c.msg();
 }
}
Output: 

Inside Class1
Hello There

We know multiple inheritances can also be achieved using the interface. In interface implementation is done in classes that's why we can achieve multiple inheritances.

Java Multiple Inheritance Using interface

If a class implements multiple interfaces or interface extends multiple interfaces, it is known as multiple inheritances.

The following example explains multiple inheritances: 

//Java Interface example
//thecodingshala.com

interface intfc1{
 void className();
}

interface intfc2{
 void className(); //same as intfc1 interface
 void msg();
}
class Class1 implements intfc1, intfc2{ //implements both the interfaces
 public void className() {
  System.out.println("Inside Class1");
 }
 public void msg() {
  System.out.println("Hello There");
 }
}

class Main{
 public static void main(String[] args) {
  Class1 c = new Class1();
  c.className();
  c.msg();
 }
}
Output: 

Inside Class1
Hello There

Question: What is the marker or tagged interface?
Answer: An interface that has no member means no fields and not methods is known as a marker or tagged interface, for example, Serializable, Cloneable, Remoter, etc.

We know that both abstract classes and interfaces are used to achieve abstraction. We can not create an object of both abstract class and interface. The following are some difference between abstract classes and java interfaces:
  • An abstract class can have abstract and non-abstract methods while the interface can have only abstract methods plus default and static method.
  • Java interface supports multiple inheritances while abstract class doesn't.
  • An abstract class can extend another Java class and implement multiple Java interfaces while the interface can extend another Java interface only.


Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Java Program to Convert Binary to Decimal - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Java Program to Convert Decimal to Binary - The Coding Shala