Definition 

  • Interface is a technique to represent multiple inheritance in java.
  • An Interface decides what a class will do but not how, on implementation.

Features:

  • An interface is 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 implements/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, interface keyword is used.
  • All methods and variables/fields of an interface must be declared public.
  • Interface variables must be initialized.
  • A class that implement/use interface must implement all the abstract method 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 an class’s object that implements an interface by the type of the interface.Thus, Interfaces can’t have constructors because we can’t create object or instantiate them.
  • An interface behaves like as a reference type for a class.
  • 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 extends keyword.
  • A java class can use/implement or use any number(one/many) of interfaces as per need.
  • 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 interface in a java class, we use implements keyword.
  • If a class implements an interface and does not provide method bodies for all functions specified in the interface, then class must be declared abstract.
  • A class cannot use/implement two/more interfaces that have same methods name but different return type.
  • It is like a blueprint/template which is actually used inside the java class.
  • Interface is like a protocol which is used to control and manage the network whereas 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 childinterfacename extends parentinterfacename

{   

      // source codes of parentInterface variables/fields.

      // source codes of childInterface variables/fields.

     // source codes of parentmethods.

   // source codes of childinterface methods.

} 

Advantages:
  • Interface gives complete abstraction features.
  • It supports multiple inheritance unlike 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 signature but no body/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 operation.

  • Functional Interface

An interface that contains only one abstract method (i.e. they can have only one functionality to exhibits) where as they may contain any number of default methods. For example- Runnable, ActionListener, Comparable etc.

  • Nested Interface-

When an interface is declared/created inside another interface as per requirement, is called nested interface. 

NB:
  • From jdk 8/Java 8, we can use default and static keyword for interface methods.
  • From jdk 9/Java 9, we can use Static methods, Private methods, Private Static methods in interface.
  • An Interface program is normally saved and executed (run), similar as normal java class-object program.

 552 total views,  1 views today

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.