Definition 

  • Interface is a technique to represent multiple inheritance in Java.
  • An Interface decides what a class will do but not how to implement it.

Features

  • An interface is a class-like structure (not a class) having abstract methods (i.e. only signature and no body), default methods, static methods, etc. as well as final variables field and static variables fields.
  • An interface may also contain constants and nested types.
  • An interface is made mainly to implement/use inside a class.
  • A Java class uses/implements the contents(the abstract methods etc.)of an interface as per requirements.
  • To declare/create an interface, the interface keyword is used.
  • All methods and variables/fields of an interface must be declared public.
  • Interface variables must be initialized.
  • A class that implements/uses the interface must implement all the abstract methods or default methods as well as non-default methods declared in the interface.
  • Interfaces are more flexible in use hence a single class can use/implement multiple interfaces as per need.
  • we can not create an object (instantiate) for an interface. We can, however, refer to a class object that implements an interface by the type of the interface. Thus, Interfaces can’t have constructors because we can’t create objects or instantiate them.
  • An interface behaves like a reference type for a class.
  • An interface cannot be declared as private, protected, or transient.
  • An interface can also be inherited or derived from one or more interfaces (like Java class) using the extends keyword.
  • A Java class can use/implement or use any number(one/many) of interfaces as needed.
  • An interface cannot be used/implemented in another Interface rather it can extend another interface if needed i.e. an interface can be inherited but not implemented (into another interface).
  • An interface resolves the conflicts/ambiguity of similar variable names and methods they have, unlike Java class.
  • To implement/use an interface in a Java class, we use the implements keyword.
  • If a class implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstract.
  • A class cannot use/implement two/more interfaces that have the same method name but different return types.
  • It is like a blueprint/template which is used inside the Java class.
  • The interface is like a protocol that is used to control and manage the network whereas the interface is used in the java class management.

Image title

Your subtitle here

Syntax: 

To Create Interface

interface interfacename

{   

    // source codes of Interface variables/fields.

    // source codes of abstract methods.

}

To Implement Interface 

Class classname implements interfacename

{   

    // source codes of Interface variables/fields.

    // source codes of interface methods.

} 

To Inherit/Extend Interface

 interface child_interface_name extends parent_interface_name

{   

      // source codes of parentInterface variables/fields.

      // source codes of childInterface variables/fields.

     // source codes of parentmethods.

   // source codes of child interface methods.

} 

Advantages:
  • The interface gives complete abstraction features.
  • It supports multiple inheritance, unlike the java class.
  • It helps to obtain loose coupling for modules present in a system.
Types of Interface:
  • Marker/Tagged Interface
    • It is an empty interface that has no variables or fields and methods.
    • They have only signatures but no bodies/codes.
    • For example- Serializable, Cloneable, Remote, etc.
    • This type of interface is used to provide some specific & essential information to the JVM to perform some useful operations.
  • Functional Interface
    • An interface that contains only one abstract method (i.e. they can have only one functionality to exhibit) where as they may contain any number of default methods.
    • For example- Runnable, ActionListener, Comparable, etc.
    • Runnable Interface: 
      • In Java, the Runnable interface is part of the java.lang package.
      • Runnable class is extensively used in network programming as each thread represents a separate flow of control. Also in multi-threaded programming, Runnable class is used. 
      • The Runnable interface is an in-built interface and is designed to be implemented by any class whose instances or tasks can be executed concurrently by a thread.
      • There are two ways to start a new Thread –  one by using subclass Thread and the other by implementing a Runnable interface.
      • The Runnable interface has a single abstract method named run(), which represents the code that will be executed when the thread is started. Classes implementing the Runnable interface need to provide an implementation for this run() method.
      • Using the Runnable interface is a common approach to implementing multithreading in Java.
  • Nested Interface-
    • When an interface is declared/created inside another interface as per requirement, is called the nested interface. 
NB:
  • From JDK 8/Java 8, we can use default and static keywords for interface methods.
  • From JDK 9/Java 9, we can use Static methods, Private methods, and Private Static methods in the interface.
  • An Interface program is normally saved and executed (run), similar to a normal Java class-object program.

Loading

Categories: Java

0 Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.