An interface in java is a blueprint of a class. It has static constants and abstract methods only.

Interface looks like class but it is not a class. An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body). Also, the variables declared in an interface are public, static & final by default. We will discuss these points in detail later in this post.

The interface in java is a mechanism to achieve fully abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve fully abstraction and multiple inheritances in Java.
Java Interface also represents IS-A relationship. It cannot be instantiated just like abstract class.


Extending Interface

Classes,,interfaces can also be extended.That is ,an interfaces can be subinterfaced from other interfaces. The new subinterfaces will inherit all the members of the super inheritanace in the manar similar to sub classes.

interface Item
{
int code =10;
string name = "veeru"
}
interface item extends itemcost
{
void display();
}

The interface item would inherit both the constants code and name into it.That the variables name and code are declared like simple variables.It is allowed all the variables in an interface are treated as constants although the keywords final and static are not present.


Uses Java interface

There are mainly three reasons to use interface. They are given below.

  1. It is used to achieve fully abstraction.
  2. By interface, we can support the functionality of multiple inheritances.
  3. It can be used to achieve loose coupling.

In java, multiple inheritance is not allowed, However by using interfaces you can achieve the same . A class can extend only one class but can implement any number of interfaces. It saves you from Deadly Diamond of Death(DDD) problem.


Declaration

Interfaces are declared by specifying a keyword “interface”. E.g.:

interface MyInterface
{
   /* All the methods are public abstract by default
    * Note down that these methods are not having body
    */
   public void method1();
   public void method2();
}

The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members.
In other words, Interface fields are public, static and final bydefault, and methods are public and abstract.


Interface Implementation

This is how a class implements an interface. It has to provide the body of all the methods that are declared in interface.

Note: Class implements interface but an interface extends another interface.

interface MyInterface
{
   public void method1();
   public void method2();
}
class XYZ implements MyInterface
{
  public void method1()
  {
      System.out.println("implementation of method1");
  }
  public void method2()
  {
      System.out.println("implementation of method2");
  }
  public static void main(String arg[])
  {
      MyInterface obj = new XYZ();
      obj. method1();
  }
}
Output:
implementation of method1

Understanding relationship between classes and interfaces

As shown in the figure given below, a class extends another class, an interface extends another interface but a class implements an interface.


Simple example of Java interface

In this example, Printable interface have only one method, its implementation is provided in the A class.

	Class Interface1
	{
	interface printable
	{  
	void print();  
	}  	  
	class A6 implements printable
	{  
	public void print()
	{
	System.out.println("Hello");
	}  
	public static void main(String args[])
	{  
	A6 obj = new A6();  
	obj.print();  
	}  
	}  

Multiple inheritance in Java by interface

If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance.

	interface Printable
    {  
	void print();  
	}  	  
	interface Showable
    {  	
    void show();  
	}  	  
	class A7 implements Printable,Showable
    {  	  
	public void print(){System.out.println("Hello");
    }  
	public void show(){System.out.println("Welcome");
    }    
	public static void main(String args[]){  
	A7 obj = new A7();  
	obj.print();  
	obj.show();  
    }  
	}  

Multiple inheritance is not supported through class in java but it is possible by interface, why?
multiple inheritance is not supported in case of class. But it is supported in case of interface because there is no doubt as implementation is provided by the implementation class. For example:

	interface Printable{  
	void print();  
	}  
	interface Showable{  
	void print();  
	}  	  
	class TestTnterface1 implements Printable,Showable
	{  
	public void print(){System.out.println("Hello");
	}  
	public static void main(String args[]){  
	TestTnterface1 obj = new TestTnterface1();  
	obj.print();  
	}  
	}  

As you can see in the above example, Printable and Showable interface have same methods but its implementation is provided by class TestTnterface1, so there is no ambiguity.


Interface and inheritance

A class implements interface but one interface extends another interface .

	interface Printable{  
	void print();  
	}  
	interface Showable extends Printable{  
	void show();  
	}  
	class Testinterface2 implements Showable{  	  
	public void print(){System.out.println("Hello");}  
	public void show(){System.out.println("Welcome");}  	  
	public static void main(String args[]){  
	Testinterface2 obj = new Testinterface2();  
	obj.print();  
	obj.show();  
	}  
	}  

In the above program my “Demo” class is implementing only one interface “Interface? however it has to provide the implementation of all the methods of interface “printable? too, because Interface extends printable.


Difference between Abstract and Interfaces

S.NO Abstract Interface
1Abstract class can hava abstract and non-abstract methodsInterface can have only abstract methods
2 Abstract calss doesn’t support multiple inheritance Inheritance support multiple inheritance
3Abstract class can have finals, non-finals ,static and non-static variables Inheritance has only static and final variables
4 Abstract class can have static methods ,main methods and constructors Inhertance can’t have static methods ,main methods or constructors method
5.Abstract class can provide the implementation of interface Interface can’t provide the implementation of abstract class.
6The abstract key word is used to declare abstract class.Interface can’t provide the implementation of abstract class.
7
 
Example:

Public abstract  class  shape 
{
Public abstract void  draw();
}
Example:

Public Interface drawble
{
Void  draw();
}

Example of abstract class and interface in Java

Let's see a simple example where we are using interface and abstract class both.

	//Creating interface that has 4 methods  
	interface A{  
	void a();//bydefault, public and abstract  
	void b();  
	void c();  
	void d();  
	}  	  
	//Creating abstract class that provides the implementation of one
method of A interface abstract class B implements A{ public void c(){System.out.println("I am C");} } //Creating subclass of abstract class,now we need to provide the
implementation of rest of the methods class M extends B{ public void a(){System.out.println("I am a");} public void b(){System.out.println("I am b");} public void d(){System.out.println("I am d");} } //Creating a test class that calls the methods of A interface class Test5{ public static void main(String args[]){ A a=new M(); a.a(); a.b(); a.c(); a.d(); } }

note:-
1)We can’t instantiate an interface in java.
2)Interface provides complete abstraction as none of its methods can have body. On the other hand, abstract class provides partial abstraction as it can have abstract and concrete(methods with body) methods both.
3)implements keyword is used by classes to implement an interface.
4)While providing implementation in class of any method of an interface, it needs to be mentioned as public.
5)Class implementing any interface must implement all the methods, otherwise the class should be declared as “abstract”.
6)Interface cannot be declared as private, protected or transient.
7)All the interface methods are by default abstract and public.
8)Variables declared in interface are public, static and final by default.

interface Try
{
   int a=10;
   public int a=10;
   public static final int a=10;
   final int a=10;
   static int a=0;
}

News Walls


Copyrights © 2016. All Rights Reserved to Learn Computers Home, India. //Site Designed.