Interface in JAVA

What you will learn here about interface in java

  • What is interface in java
  • How to define interface in java
  • Important points about interface in java
  • interface example in java
  • interface extends interface example in java
  • Why we use interface in java

What is interface in java:

Generally, the interface is a communication media between two things.  For example in communication between you and a mobile touchpad is an interface or in communication between you and laptop mouse and keyboards are the interfaces. But programmatically interface is the one which has only abstract methods. The interface is also called a 100% abstract class since it has only abstract methods. The interface is also a data type. Since java 8 we can define default and static methods in interface.

How to define interface in java

In java interface is defined using keyword called interface. General syntax is given below.

interface syntax

public interface sample
{
//define abstract method here
}

Important points about interface in java

1)The interface has only abstract methods and variables.  An abstract method is the one which has only a method declaration and there is no method implementation which is shown below.

2) The interface is created by using the keyword interface.

3)  We can not create the object of the interface.

4) In Java, every class extends super most class called Object but the interface does not extend the super most class called Object class.

 5) A Class can access the properties of an interface using keyword implements which is shown below. Graphically implements is shown the dotted line where arrow points from subclass to parent interface. 

6) An interface can access the properties of another interface using the keyword extends which is shown below. Graphically extends is shown the solid line where arrow points from subinterface to parent interface.  

7) By default, variables declared in an interface are final and static means variables are constant which is shown below.

8)  If we don’t specify any access modifier for a method then by default it will be public and abstract which is shown below.

9) When class accessing the properties of the interface then overriding of all abstract method is mandatory in class. If you don’t override all the abstract methods then you need to declare the class as an abstract class which is called partial implementation.

10)Signature of the abstract method must be the same as of its implementation means return type and argument type of abstract method must be the same as return type and argument type of implemented method.

11) One class can implements multiple interfaces simultaneously by separating interfaces using a comma. 

12) Multiple inheritance is possible using an interface.

13) Nesting of an interface is possible. When you define an interface within a class is called nesting interface which is shown below.

interface example in java

The class extending the interface using implements keyword example in given below

interface extends interface example in java

Class access the properties of an interface using keyword called implements and interface access the properties of another interface using keyword called extends which is shown below.

Why we use interface in java

Loose coupling:  Change in implementation which does not affect the client or user is called loose coupling. For example, we know the keyboard is the interface between you and the computer. Tomorrow if you replace your computer with another computer, still you can communicate with your new computer using the existing keyboard. Which is nothing but the loose coupling.

Abstraction: Hiding the method implementation and providing method functionality is nothing but the abstraction.

Multiple inheritance achieved as one class can implements one or more than one interfaces simultaneously.

 

 


Sangeetha S M


This post is contributed by Sangeetha S M. If you like to contribute bytesofgigabytes, you can also write an article using bytesofgigabytes contribute . See your article appearing on the bytesofgigabytes.com main page to help other people.

 

You may also like...

Leave a Reply