WHAT IS INTERFACE ?

When you create an interface, you're defining a contract for what a class can do,
without saying anything about how the class will do it.
interface demo{
int x=10;
void fun();
}

We can never define the method body in an interface.
The variable initialization is mandatory.

A class can implement an interface but an interface can extend another interface.
interface A{}
class B implements A{}
interface C extends A()


SOME INTERFACE RULES-
-All interface methods are implicitly public and abstract. In other words,
you do not need to actually type the public or abstract modifiers in the
method declaration, but the method is still always public and abstract.
interface Bounceable{
public abstract void bounce( );
//void bounce();//both methods are legal
}
-All variables defined in an interface must be public, static, and final—
in other words, interfaces can declare only constants, not instance variables.
interface Foo {
int BAR = 42;//initialization is mandatory
void go();
}
class Zap implements Foo {
public void go() {
BAR = 27;
}
}
this will be compilation error because the variable BAR is by default final we can not modify its value.
-Interface methods must not be static.
-Because interface methods are abstract, they cannot be marked final,
strictfp, or native.
-An interface cannot implement another interface or class.

5 comments:

  1. Interface contains o or more abstract methods.but not contains the concrete methods.

    ReplyDelete
  2. why the interface variable are static?

    ReplyDelete
  3. Becoz we cant create object for an interface....and the variable is final by default.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  4. Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.java training in chennai | chennai's no.1 java training in chennai | best java institute in chennai

    ReplyDelete