Interface in java

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

public interface MyInterface
{
int i=0;
public void Height(int height);
public abstract void setHeight();

}

Java 8 Interface Example

package com.jbt;

public interface Interface_JAVA8 {

	public void method();

	public abstract void method1();

	public default void method2() {
	}

	public static void method3() {
	}
}

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:

public class InterfaceExampleOne implements interfaceOne {

}

interface interfaceOne {

}

 

Example 2:

/*
 * As implmeneted Interface have any abstract method so this class
 * need to implement any method.
 */
class InterfaceExampleTwo implements interfaceTwo {

	@Override
	public void methhod() {
		System.out.println(var);
	}

}

/*
 * Below interface has an abstract method so implemented class needs to
 * implement this method unless and untill it is abstract itself
 */
interface interfaceTwo {
	public final int var = 9;

	public abstract void methhod();
}

Example 3:

/*
 * As below class is not abstract class and it is extending abstract class which
 * has not yet implemented the method from interface so this class is FORCED to
 * implement method from Interface in hierarachy(interfaceTwo).
 */
class InterfaceExampleTwo extends InterfaceExampleThree {

	@Override
	public void methhod() {
		System.out.println(var);
	}

}

/*
 * Below interface has an abstract method so implemented class needs to
 * implement this method unless and untill it is abstract itself
 */
interface interfaceTwo {
	public final int var = 9;

	public abstract void methhod();
}

/*
 * Even if Interface has abstract method ABSTRACT CLASS is not forced to
 * implement it. Abstract class may/may not navigate this responsibility of
 * implementing abstract method to class which is not abstract.
 */
abstract class InterfaceExampleThree implements interfaceTwo {

// Method from Interface is not implemented here

}

 

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 >>
By | 2017-07-09T12:11:21+00:00 April 20th, 2012|Core Java|20 Comments

About the Author:

20 Comments

  1. rajesh November 12, 2012 at 7:22 am - Reply

    pls check out there is no link for Abstract Class.
    Thank You.

  2. yasaswy September 12, 2013 at 10:41 am - Reply

    a link for abstract class would help
    thanks

  3. karthikeyan November 21, 2013 at 7:14 am - Reply

    Nice Article

    • Vivekanand Gautam November 23, 2013 at 5:56 am - Reply

      Thanks for your nice comment.

  4. Lavanya April 24, 2014 at 9:16 am - Reply

    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 .

  5. Arielle July 6, 2014 at 12:56 pm - Reply

    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

  6. puja September 7, 2014 at 5:59 pm - Reply

    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.

    • karthick June 24, 2015 at 2:32 am - Reply

      Puja

  7. infoj August 10, 2015 at 5:54 pm - Reply

    Starting from Java 8 interface can have default methods and static methods as well.

    • J Singh July 6, 2016 at 10:51 am - Reply

      TRUE. I have updated the article accordingly.

  8. mack September 30, 2015 at 3:09 pm - Reply

    Give me an example of student mark calculation using interface

  9. Ajit January 11, 2016 at 2:22 pm - Reply

    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.

    • Ajit January 11, 2016 at 2:26 pm - Reply

      Or other way it can be “A class can Implement more then one Interfaces”

    • J Singh July 6, 2016 at 10:50 am - Reply

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

  10. Hari Naaraayanan U.G June 24, 2016 at 11:12 am - Reply

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

    • J Singh July 6, 2016 at 10:53 am - Reply

      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.

  11. HEMANTH BOLLAMREDDI July 5, 2016 at 4:50 pm - Reply

    ” 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

    • J Singh July 6, 2016 at 10:54 am - Reply

      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.

  12. HEMANTH BOLLAMREDDI July 5, 2016 at 5:10 pm - Reply

    ALSO IN THE SAME CODE I ACCESSED THAT METHOD AS BE STATIC

    • J Singh July 6, 2016 at 10:54 am - Reply

      Yes. As it was static you can provide body part to methods.

Leave A Comment