What do you Understand with Interface in Java Programming Language?


What do you understand with INTERFACE word?
Till Java 7, Interfaces are quite similar to Classes of java but, you cannot create instance of an Interface and their methods are declared without any body.
We know there are four main features of Object Oriented Language one of them is Abstraction.
Abstraction means
you have only knowledge about methods signature or say what that method will do but don’t know how it’ll complete that task for which it is made by programmer.
Example of Abstraction:
Imagine you have a method name stepsToGoShoppingMart(); but, you don’t know what are the steps to go Shopping Mart or you can say you don’t have a specific knowledge about what are the statements in this method. i.e; Abstraction.
So we can say Interface is used to achieve COMPLETE abstraction in java. When you create an interface it defines what a class can do without saying anything about how the class will do it.
  interface <nameofyourinterface>
Example of Interface:
 interface Finally_got
          int average_speed=40;
          void move();

Compiler automatically converts methods of Interface as public and abstract, and the data members as public, static and final by default.
Rules for using Interface:
      Methods inside Interface must not be static, final, and native till java 7 but in java 8 we can have static methods but only for those methods having complete definition so that those cannot be overridden.
        All variables declared inside interface are implicitly public static final variables (constants).
      All methods declared inside Java Interfaces are implicitly public and abstract, even if you don't use public or abstract keyword.
        Interface can extend one or more other interface.
        Interface cannot implement a class.
        Interface can be nested inside another interface.

Example of Interface implementation:
interface Finally_got
      int average_speed = 40;
      void move();
class Vehicle implements Finally_got
     public void move()
             System.out.println("Average speed is "+average_speed);
     public static void main (String[] arg)
             Vehicle vc = new Vehicle();
Output is: Average speed is 40

If you come so far that means you were engaged in reading my article.

Please Share with your friends to make them understand and feel us confident to write more for you.
Thank you.
Co-Author : Bharti Bhati (B.sc. (H) Computer Science)

You Might Also Like


If you have any questions or suggestions, you are free to ask, i will appreciate that and, i will try my best...

Check Out