·  JAVA – Syntax – Terms – Interface

 

- Info:

  - Interface is collection of methods which can be used by someone else to comunicate.

  - In Java interface is declared using keyword interface.

  - interface keyword declares constants and method declarations, but not method implementations.

    Java interfaces are meant to replace multiple inheritance.

  - implements defines that class must implements functions from given list of interfaces.

  - To instantiate a class that implements interface, that class has to give body to all functions from interface.

  - This ensures that all classes that implement the same interface have the same set of functions (interface).

 

- Procedure:

  - Create JAVA Console Application Test.java

 

    Test.java

    public class Test{ 

      public static void main(String[] args) {  

        Instrument instrument = new Instrument();

                   instrument.sound();              

      }

    }

    

    interface SoundInterface {

      public int  i=10;

      public void sound();

    }  

 

    interface GravityInterface {

      public double g = 9.81;

      public void   fall();

    }  

    

    class Instrument implements SoundInterface, GravityInterface {

      public void sound() { System.out.println("Playing note"        ); };

      public void fall () { System.out.println("Falling from the sky"); };

    }