Thursday 23 April 2015

Interface In Java

Firstly ,

What is this interface in java ?



An interface in java is a blueprint of a class. It has static constants and abstract methods only.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 inheritance in JavaAn interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors(Abstract Methods) that a class implements.
Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.Since multiple inheritance is not allowed in java so interface is only way to implement multiple inheritance.

Why interface is not a class ?
An interface is similar to a class in the following ways:
  1. An interface can contain any number of methods.
  2. An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
  3. The bytecode of an interface appears in a .class file.
  4. Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

  But,



  an interface is different from a class in several ways, including:
  1. You cannot instantiate an interface.
  2. An interface does not contain any constructors.
  3. All of the methods in an interface are abstract.
  4. An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
  5. An interface is not extended by a class; it is implemented by a class.
  6. An interface can extend multiple interfaces.

    So, Interface is not a class .


Properties Of An Interface :-


  • An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface.
  • Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
  • Methods in an interface are implicitly public.


Now we understand the properties of an abstract class with diagrams 






Java compiler adds public and abstract keywords before the interface method and public,static and final keywords before data members.


Relationship between Class and Interface :-

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





Multiple Inheritance In Java :-


If a class implements multiple interfaces, or an interface extends multiple interfaces .This is known as Multiple Inheritance 

Multiple inheritance is not supported in case of class. But it is supported in case of interface because there is no ambiguity as implementation is provided by the implementation class

A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface.The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.

class testinterface1 implements Printable,Showable



Now let understand this with a program 

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

Output:-
Hello

Printable and Showable interface have same methods but its implementation is provided by class, so there is no ambiguity.

Interface Inheritance :-

When one interface extends another interface then this is called interface inheritance .Lets take an example to understand this 

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();  
 }  
}

Output Will be :-
Hello
Welcome


Some Essential Rules :-

When overriding methods defined in interfaces there are several rules to be followed:
1).   Checked exceptions should not be declared on implementation methods other than the ones declared by the interface method or subclasses of those declared by the interface method.
2).   The signature of the interface method and the same return type or subtype should be maintained when overriding the methods.
3).    An implementation class itself can be abstract and if so interface methods need not be implemented.

When implementation interfaces there are several rules:
1).   A class can implement more than one interface at a time.
2).   A class can extend only one class, but implement many interfaces.
3).   An interface can extend another interface, similarly to the way that a class can extend another class.


No comments:

Post a Comment