Interface in java

This entry is part 18 of 34 in the series Core Java Course

Defining a Contract means to create an Interface. This Contract states what a Class can do without forcing how it should do it.

Declaring an Interface

Interfaces can be defined with the Interface keyword. After Java 8, Interface definition changes a lot.

Java Interface Example

Java 8 Interface Example

Java 8 introduces a concept of Default Method. Now an Interface can contain Method body if it is not abstract. A  method inside Interface can contain either of “abstract”, “static” or “default” modifier.

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 use keyword it will not create the problem as you can see in the second Method declaration. (Before Java 8)
  • Interfaces can declare only Constant. Instance variables are not allowed. This means all variables inside the Interface must be public, static, final. Variables inside Interface are implicitly public static final.
  • Interface Methods can not be static. (Before Java 8)
  • Interface Methods can not be final, strictfp or native.
  • The Interface can extend one or more other Interface. Note: The Interface can only extend another interface.

Rules for Declaring Interface(JAVA 8)

Interface logic has been changed in Java 8. Hence some of the above logic doesn’t apply on Interface after Java 8.

  • All Interface Method can have either “abstract”, “static” or “default” modifier.
  • Interface methods can have body if static or default modifier is used against method.

Interface vs Abstract Class

An Interface is like having a 100% Abstract Class. Interfaces can not have non abstract Methods while abstract Classes can. A Class can implement more than one Interface while it can extend only one Class. As abstract Classes comes in the hierarchy of Classes, they can extend other Classes while Interface can only extend Interfaces.

 

Use Interface in Class

How can we take advantage of an Interface after creating it in Java? To take advantage we need to implement our Class with a given Interface. The Implement  keyword can be used for this purpose.


Examples of Class Implementing Interface

Example 1:

 

Example 2:

Example 3:

 

Bullet Point

  • Interface are 100% abstract class(Implicitly). After Java 8 it doesn’t hold true.
  • Interfaces can be implemented by any class from any inheritance tree.
  • All methods in Interfaces are abstract. (In Java 8 either abstract/ static / default)
  • Interface can have constants, these constants are public, static and final(Implicitly).
  • Interface methods are implicitly public & abstract. (Before Java 8)
  • Class implementing an interface can also be an abstract class.
  • An abstract class which is implementing an interface need not implement all abstract method.
  • A class can Implement more then one Interface.
  • Interfaces can not extend a class or implement an Interface.
  • Interface can extend another Interface.
  • A non abstract class which is implementing an Interface needs to follow some rules .
    1. This class needs to provide concrete implementation of all abstract method.
    2. All rules of Overriding needs to be followed.
    3. It must maintain the exact signature of method.
Series Navigation<< Java Static KeywordOverloading in java >>

20 Comments Interface in java

  1. Lavanya

    Given examples are very understandable. But the explanation would be more precise. And u r using “Then” in place of “Than” in so many places. Please check once .

    Reply
  2. Arielle

    It’s hard to find your website in google. I found it on 18 spot, you should build quality
    backlinks , it will help you to increase traffic. I know how to help you,
    just search in google – k2 seo tricks

    Reply
  3. puja

    what is actual use of interface?its compulsory in every class to use interface
    I not get understand below line..
    1.Creating an Interface means defining a Contract
    2.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.

    Reply
  4. Ajit

    I think the 8th point “A class can extend more then one Interface” should be changed to “An Interface can extend more then one Interface”. Please correct me if I am wrong.

    Reply
    1. J Singh

      It was supposed to be “A class can implement more than one interface.” Same has been rectified and updated. Thanks for pointing this typo.

      Reply
  5. Hari Naaraayanan U.G

    Contradiction occurs here….
    Interfaces can not extend a class or implement an Interface.
    Interface can extend another Interface.

    Reply
    1. J Singh

      Its not contradiction. If you read the line you will get what i am trying to say.
      Interface can not extend a class.
      Interface can not Implement an Interface.
      Interface can extend an Interface.
      Hope it clarifies now.

      Reply
  6. HEMANTH BOLLAMREDDI

    ” All interface Methods are implicitly public and abstract. ” IT IS NOT TRUE BECAUSE I WROTE A BODY TO THAT METHOD AND IT WORKED FINE EVEN WITHOUT A WARNING

    Reply
    1. J Singh

      Thats because you are using Java 8. In java 8 rules have been changed for Interface. Now Interface methods can have body part if it is defined as static or default. I have updated the article accordingly for future reference.

      Reply

Leave A Comment

Your email address will not be published. Required fields are marked *