Interface in Java - Study24x7
Social learning Network
study24x7

Default error msg

Login

New to Study24x7 ? Join Now
Already have an account? Login

Interface in Java

Updated on 15 February 2022
study24x7
Java Object oriented programmi
7 min read 3 views
Updated on 15 February 2022

The interface is like class having variables and methods. Unlike class, methods are in interface not complete. i.e only declaration is given not the body.


It is used to achieve oops one of the important features Abstraction. Abstraction is hiding the Business Logic (important implementation) from the end-user and giving only functionality for use to the end-user. 



There are numerous techniques to achieve abstraction in java.

 1 By implementing an interface 

 2 By extending Abstract class


The interface is the blueprint of a class. Writing the interface is similar to writing the class, As class shows the attributes and behaviors of any object, An interface shows how the class has gone behave after implementing the interface. 


Why use an interface in JAVA


For security: Interface is in such as nature it hides the formal details from the objects.


For achieving multiple inheritances: In java, more than one class can’t extend then we use interface in such a case. We will explain this in an easy example. Suppose we are going to implement multithreading in class which already extends some other class so for achieving the properties of multithreading we have to implement Runnable interface.so my previous inheritance maintains as well as multi-threading is achieved. 


By using interface keyword we declare an interface. 


interface name_of_the_interface

{

// constant fields

// by default.

//static metheod

// methods that abstract 

}


Note:

➡ Since Java 8, an interface can have static & default methods.

➡ There is no need to use the abstract keyword while declaring an interface because the interface is implicitly abstract.

➡There is no need to use abstract keywords for a method as they are implicitly abstract.

➡ Methods in an interface are implicitly public.

➡ An interface can extend another interface. It similar to the class extends. For inheritance, the interface extends keyword is used.

Example:


public interface A{

  public void printParentInterface ();

}


// Interface inherit the another interface

public interface B extends A{

  public void printChildInterface ();

  public void printInterface ();

}


// class implements interface

public class Ex_Interface implements B{

    /**

     * Implements All 3 metheods

     */

  public void printParentInterface (){

    System.out.print ("Parent interface")

  }

  public void printChildInterface (){

    System.out.print ("child interface.");

  }

  public void printInterface (){

    System.out.print ("interface B");

  }

}


Class Ex_Interface have to implement all methods in B and A


// Main class

public class main{

  public static void main (String s ...){

    Ex_Interface ex_interface=new Ex_Interface();

    // Calling the function

    ex_interface.printParentInterface();

    ex_interface.printInterface();

  }

}


What is the marker interface in Java?

It is a very famous and important question related to an interface. An interface in java having no fields call marker interface. An empty interface(no methods or field) is called the marker interface. It is used for just giving information to the java compiler. Cloneable, Serializable, Remote, etc are some examples of marker interface.


public interface A 

{

  // empty nothing is 

}


Some important point on the interface


➡ Can an interface have a constructor

Interfaces can’t have constructors, So we achieve 100% abstraction by an interface.

Can we declare an Interface with an “abstract” keyword?

Yes, But it is useless because the interface is abstract by default.

After compilation of interface, the .class file will be generated. Yes or No?

Yes, .class file is generated because interface behaves like class.

Like classes, does interfaces also extend Object class by default?

No, Interface by default doesn’t extend object class.



study24x7
Write a comment...
Related Posts