Java Interface

Categories: Java // Tags: , , .
Email this to someonePrint this pageShare on Google+2Share on Facebook5Tweet about this on TwitterShare on LinkedIn1Share on Reddit0Pin on Pinterest3Share on StumbleUpon1


Interface

Creating an Interface means defining a Contract. This Contract states what a class can do without forcing how it should do.

Java Interface

Declaring an Interface
Interface can be defined with Interface keyword.

Java Interface Example

Rules for Declaring Interface
There are some rules that needs to be followed by Interface.

  • All Interface methods are implicitly public and abstract. Even if you write these keyword it will not create problem as you can see in second method declaration.
  • Interfaces can declare only Constant. Instance variables are not allowed. It means all variables inside Interface must be public, static, final. Variables inside interface are implicitly public static final.
  • Interface methods can not be static.
  • Interface methods can not be final, strictfp or native.
  • Interface can extend one or more other interface. Note: Interface can only extend other interface.

Interface vs Abstract Class

Interface are like 100% Abstract Class. Interface can not have non abstract methods while abstract class can have. A Class Can implement more than one interface while Class can extend only one class. As Abstract class comes in hierarchy of Classes it can extend other classes too while Interface can only extend Interfaces.

Use Interface in Class
How can we take the advantage of Interface after creating it in Java. To take the advantage we need to implement or class with given Interface. Implement keyword can be used for this purpose.

Examples of Class Implementing Interface
Example 1:

Example 2:

Example 3:

Email this to someonePrint this pageShare on Google+2Share on Facebook5Tweet about this on TwitterShare on LinkedIn1Share on Reddit0Pin on Pinterest3Share on StumbleUpon1

About Team

Browse Archived Articles by Team

  • Hi to all, since I am really keen of readding this weeb site’s
    post too be updated regularly. It inclpudes good material.