Oct 13, 2014

Internals of Java Interface

Interface Internals in JAVA

Little description of java internals about Interface: Some people say, Interface is used to implement the concept of Multiple Inheritance,but purpose of Interface is not implementing the concept of Multiple Inheritance in Java, although it looks like Multiple Inheritance.

Apart from this It is very important to know that why do we need Interface? The answer is that, If we know about the entity, but don't know about the behavior then we need Interface.
A very simple example of Interface is as follows-

interface Animal
 {
   void eat ();
  }
class Goat implements Animal
  {
    Void eat ()
      {
        System.out.println("Goat is eating grass");
      }
  }
class Dog implements Animal
  {
       void eat()
       {
         System.out.println("Dog is barking");
       }
 }

Some important points for Interface-
1. Interface is a contract.
2. Interface is a contract between service provider and service utilizer.
3. Interface is a keyword of Java.
4. Interface is a user defined data type.
5. Interface is a reference data type.
6. Interface is also structure of java, because it generate .class file. i.e byte code.
7. Interface is the collection of zero or more than zero abstract method and zero or more           than zero final variable.
8. Object of Interface cannot be created.
9. Interface is a pure abstract class, because it only contains abstract method, not any               concrete method. 
10. Interface is a pure SUPER class.

Characteristics of Interface-
1. Method of Interface by default is an abstract.
2. Each and every method of Interface by default declared as a 'public abstract'. If you don't declare Interface as a public abstract,then it is duty of Java Compiler to use 'public abstract' with each method Interface. If we use any other access specifier with Interface method then compile time error is generated.
3. Each and every variable of Interface must be declared as 'public static final', if you don't use then it is duty of Java Compiler to use 'public static final' with each variable of Interface.

Note- 'implements' keyword is used to implement the property of interface in the class.

Working with JAVA Interface

There are some variations which are as follows-

1. One Interface extends another Interface

interface I1
{
}

interface I2 extends I1
{
}

2.one interface can extends zero or more than zero interface.

interface I1
{
}

interface I2
{
}

interface I3
{
}
interface I4 extends I1,I2,I3
{
}

3.one interface cannot extends/implement any class.

4.one class extends only one class and implements zero or more than zero interface.

class H
{
}

class J extends H implements I1,I2,I3,I4
{
}


Keywords used with Interface


1. public
2. abstract
3. default(by default)
4. strictfp
5. public abstract/abstract public