^

Interface

As you know, an abstract class can have abstract as well as concrete methods. An abstract class can also have variables, which can be made public, final and static explicitly. In other words, these are not public or static or final by default.
An interface is like a class having abstract methods only. In other words, an interface contains only abstract methods and no concrete methods like an abstract class.
An interface can have :
  • Variable Declaration
  • Method Meclaration
  • Variable Declaration

    Variable declared inside interfaces are public, static and final by default. You don’t have to use the keywords explicitly like in classes, for example:
      interface A
        {
          int x = 0; //public, static and final by default
        }
    If you write: public static final int x=10;
    this has same eff ect as: int x = 10;
    But, if you write int x; only, it is not valid because a final variable must be initialized at declaration time in an interface, whereas it is optional in abstract class, because it can also be initialized inside a constructor.
    Some valid and invalid variable declaration syntaxes are listed below:
    interface learn
       {	
    	public int X = 10;		// Valid
    	int y;				// Invalid (Must be initialized)
    	public static final int Z = 20;	// Valid
    	public static int A = 30;		// Valid
    	private int B = 40;		// Invalid
    	private static final int C = 50;	// Invalid
    	protected int D = 60;		// Invalid
    	int E = Z + X;			// Valid
    	int F = Z + F;			// Invalid   (Forward Reference)
       }    
    
    Due to public accessibility, variables of an interface are accessible every where and due to static modifier, these can be used in classes and interfaces using interface name as shown below:
    interface A
       {	
    	int x = 10;
       }
    interface B
       {
    	int y = 20 + A.x;	// Using variable of interface A
       }
    class C
       {
    	public static void main(String args[])
    	    {
    		int z = A.x + B.y;
    		System.out.println(z);
    	    }
       }
    

    Method Declaration:

    Methods declared inside interface are public and abstract by default. Abstract methods are also called incomplete methods.
    These methods set a provisional standard for the class, that uses the interface. it means that the class must override the abstract methods to defi ne their behaviour to be performed on calling.
    A class can use an interface using keyword implements. As a class implements an interface, it must override all the methods, otherwise, it becomes abstract and therefore, cannot be instantiated.
    While method overriding, their accessibility specifier should be public.
    If a class implements multiple interfaces containing same method declaration, then it needs to be overriden only once as shown in the following program:
    interface A
       {	
    	void fun();
       }
    interface B
       {
    	void fun();
       }
    class C implements A, B
       {
    	public void fun() //overridden once
    	    {
    		System.out.println(“Hello”);
    	    }
       }
    An interface cannot be instantiated but its reference variable can refer to object of its implementation classes. For example:
    interface Shape
       {	void area(int L, int B);
       }
    class Rect implements Shape
         {	     
         public void area (int L, int B)
    		{	
    		     System.out.println(“Area of Rect = ” + L * B);
    		}
    	 }
    class IDemo
    	{   public static void main (String args[])
    	    	{	
                		Shape S = new Rect(); //valid 	
    			S.area (10, 20);       // Area of Rect = 200
    			}
    	}

    Multiple Inheritance

    Multiple inheritance is not allowed in Java to avoid ambiguity issues. But multiple inheritance is applicable on interfaces, as shown below:Multiple inheritance is not allowed in Java to avoid ambiguity issues. But multiple inheritance is applicable on interfaces, as shown below:
    real java multiple interface
    real java multiple interface

    Multiple Implementation

    A class can not extend multiple classes but multiple interfaces can be implemented directly. Now the implementation class need to override all the methods declared inside the interfaces. If a method is common in multiple interfaces, the class need to override it only once and no ambiguity issue rises. But if multiple nterfaces consist of variables with identical names, then it will generate the error of ambiguity on their use in implementation class. This error can be resolved using fully qualified name as shown below:
    interface A
      {	
      int x = 10;
      void show();
      }
    interface B
          {	   int x = 20;
    	   void show();
          }
    class C implements A, B
       { 
        public void show()
    	{    
     	       System.out.println(“ x =  ”  + x);    // Error of Ambiguity
    			//Using fully qualified name
    	       System.out.println(“ x =  ”  + A.x);    // Ok (x = 10)
    	       System.out.println(“ x  = ”  + B.x);    // Ok (x = 20)
    	}
       }

    Difference Between Abstract Class and Interface

    Difference between abstract class and interface is common in exams and Java interviews. Using both abstraction can be achieved but still there are some significant differences, as listed below:
    • Java interfaces are used to decouple the interface of some components from the implementation. Abstract classes are typically used as base classes for extension by subclasses.
    • Opposite to abstract class, variables declared inside an interface are public, static and final by default.
    • In comparison with abstract class, methods declared inside an interface are public and abstract by default.
    • An abstract class can have abstract as well as concrete methods, whereas , an interface can have only abstract methods.
    • An interface can be implemented using keyword “implements” but an abstract class can be extended using keyword “extends” .
    • Abstract class can have constructors but interface can’t have constructors.
    • Abstract class methods can have access modifiers as public, private, protected & static but in interfaces, methods are implicitly public and abstract; we can’t use any other access modifi ers with interface methods.
    • An abstract class can extend another class and implement multiple interfaces but an interface can only extend other interfaces.
    • An interface can extend multiple interfaces, whereas, an abstract class can extend only one class.

    Default Method - JDK 8

    Sorry! It will be updated soon!!!

    Static Method - JDK 8

    Sorry! It will be updated soon!!!

    Marker Interface - JDK 8

    Sorry! It will be updated soon!!!

About the Author
Rajesh K. Bansal (SCJP-Sun Certified Java Programmer)
17 Years experience in Training & Development. Founder of realJavaOnline.com, loves coding in Java(J2SE, J2EE), C++,PHP, Python, AngularJS, Android. If you like tutorials and want to know more in depth about Java , buy his book "Real Java" available on amazon.in.
#Email : [email protected] #Contact : 98722-46056
Available on Amazon
Card image cap
Under the guidance of Founder & Author of "realJavaOnline.com". M:9872246056
Card image cap