Interface in Java

 To achieve a complete abstraction in Java, interface is defined. An interface in java can only contain static constants and abstract methods, which means we cannot define the body of a method inside an interface.

Basically, interface is a blueprint of a behavior, and it specifies what a class should do.

To define interface in Java, we use interface keyword as:

        interface Program{
            final int pid = 10101;
            void run();
        }

Note: 
  • In an interface, all the fields are public, static and final by default, methods are abstract by default.
  • A class that implements an interface must implement all the methods declared within interface. 
  • In case, if you want to define a class which implements the interface but not its method then the class must be an abstract class.

To implement an interface use implements keyword as:
        class HelloWorld implements Program{
                public void run(){
                    System.out.println("Hello world program is running...");
                }
        }

The main advantage of using interfaces in Java is that In Java multiple inheritance is not allowed, so you can use interface to implement more than one interface.

You can define method body by declaring the method as default in an interface as:
        interface Program{
                final int pid = 10101;
                void run();
                default void waiting(){
                    System.out.println("Waiting...");
                }
        }

        class HelloWorld implements Program{
                public void run(){
                    System.out.println("Hello world program is running...");
                }
        }

        public class Sample{
            public static void main(String args[]){
                    HelloWorld p = new HelloWorld();
                    p.waiting();   //Output: Waiting...
            }
        }

We can also declare a static method inside an interface, which allows us to access the method without creating any object.

        Example:
            interface Program{
                final int pid = 10101;
                static void waiting(){
                    System.out.println("Waiting...");
                }
            }

            public class Sample{
                public static void main(String args[]){
                        Program.waiting();   //Output: Waiting...
                }
            }

Note: Difference between default and static methods are that default method can be overridden while static method cannot be overridden.

Inheritance in interface:
          We know that multiple inheritance for classes is not allowed in Java, but inheritance allows extending one or more interfaces. 
          We can inherit an interface using extends keyword.

          Example:
                  interface A{
                        //Statements
                 }

                 interface B{
                       //Statements
                }

                interface C extends A, B{
                     //Methods of A, B interface will be accessible inside this interface 
               }

Continue Learning: Dynamic Binding

  

Comments

Popular Posts