Defining a Contract means to create an Interface. This Contract states what a Class can do without forcing how it should do it. In this Java Interface Tutorial, we will talk about Interfaces. How to create one and rules applied to Interfaces.
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.
Java 9 Interface Private Method
In Java 9 you are allowed to have private methods in Interfaces.
Rules for Declaring Interface
There are some rules that need 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 cannot be static. (Before Java 8)
- Interface Methods cannot 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 a body if static or default modifier is used against the 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
- An interface is 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)
- An interface can have constants, these constants are public, static and final(Implicitly).
- Interface methods are implicitly public & abstract. (Before Java 8)
- An interface can also have private methods. (Java 9)
- A 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 than one Interface.
- Interfaces can not extend a class or implement an Interface.
- An interface can extend another Interface.
- A non-abstract class which is implementing an Interface needs to follow some rules.
- This class needs to provide the concrete implementation of all abstract method.
- All rules of Overriding needs to be followed.
- It must maintain the exact signature of a method.
- After Java 9 changes, Interfaces look a lot like Abstract Class, still, there are some differences.
- An abstract class can have variables with different modifiers which is not constant
- Methods in Abstract can have the different signature than just private or public
Why Java 8, Java 9 implemented these new rules for interfaces concept? What is the advantage of this new rules in interfaces over the past interface rule
Hi Shijo, Some or design decisions and some are provided for backward compatibility purposes. It will require some time to write in detail about those changes.
I Will see if I can find some time to do that.
Nice Article. Very helpful for beginners. Thank you :)..
The interface is a mechanism to achieve fully abstraction in java. There can be only abstract methods in the interface.Many thanks for sharing this.
ALSO IN THE SAME CODE I ACCESSED THAT METHOD AS BE STATIC
Yes. As it was static you can provide body part to methods.
” 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
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.
Contradiction occurs here….
Interfaces can not extend a class or implement an Interface.
Interface can extend another Interface.
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.
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.
Or other way it can be “A class can Implement more then one Interfaces”
It was supposed to be “A class can implement more than one interface.” Same has been rectified and updated. Thanks for pointing this typo.
Give me an example of student mark calculation using interface
Starting from Java 8 interface can have default methods and static methods as well.
TRUE. I have updated the article accordingly.
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.
Puja
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
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 .
Nice Article
Thanks for your nice comment.
a link for abstract class would help
thanks
pls check out there is no link for Abstract Class.
Thank You.