What is Interface In Java? | Java interface tutorial

By | November 23, 2018

Interface In Java

Interface In Java

Interface

 

Interface In Java:

In this Java interface tutorial, I am teaching you what is an interface in java?.  Interface in java is just like a template. A template we use in MS Word.

Syntax:

Interface  InterfaceName {

Variables

Functions

}

The Syntax of Interface in java is same just like a class but you have there is some difference between interface and class.

 

 

 

Difference Between Interface and Class:

The difference between Interface and class is that in class we use class keyword and in Interface, we use Interface keyword.

In Class, we use extend keyword for extending a class. But in Interface, we use implement keyword while using interface.

One class can implement more that one Interfaces. So multiple Inheritance concepts implement here.

If there are one class x and two interfaces y and z. Then the class can implement these two Interfaces.

By default, all fields in an interface are public static and final.

But in a normal class all fields are not public static and final.means it can be public, private or final.

Methods:

All methods have a body in class but in Interface there only a signature.

In class, methods are not abstract but in Interface all methods are abstract.

Objects:

In class, we can create an object of class but in Interface, we cannot create an object.

 

Keywords:

In class, we can use public, private and protected keywords but in Interface, we can use an only public keyword.

Interface:

In interface by default all the variables are public  static and final;

public means it can be publicly used. It is access specifier.

Static means class variable.

final means we cannot change its value.

public static final type variable name = value;

e.g public static final int a =100;

when we create variables then at the time of declaration we are giving value means we initialize it. Because it is

constant.

To distinguish between normal variable and between constant variables we write a variable name in Capital letter.

e.g  public static final int A=100;

If you declare and initialize variable in Interfaces like without public static and final keyword that will be ok because by default it is public static and final.

e.g  int A=100;

You can also write like this:

static final int A=100;

final int A=100;

int A = 100;

I have told you above that by default it is written. If you write these keywords or not that will be ok.

Methods in Interface:

By default methods in an interface are abstract you do not need to use the abstract keyword with methods in Interface.

You can write methods in an interface like that.

public return type methodName(parameters);

public int  addition(int a ,int b);

You can write its signature but you cannot write it’s body in an interface. Because it is by default abstract.

By default it is public so you do not need to write public keyword with a method.

e.g    int addition(int a, int b);

Implement Keyword:

When a class uses an interface then it uses implement keyword means when you use interface in any class you have to write interface keyword.

example:    if we have written interface namely XYZ and we use it in our class B so we will write our class like that.

e.g      class B implements XYZ{

}

 

 

 

Inheritance in Interface:

When a class inherits other class we use extends keyword. We also use extends keyword when one Interface inherits other interfaces.

example: If there is two Interface  A and B so it can be written like

Interface A extends Interface B{

}

one Interface can extend many interfaces.but one class can extend only one class.

So don’t be confused with that one class can implement many interfaces.

 

 

 

 

See also:

Classes and objects in Java

2 thoughts on “What is Interface In Java? | Java interface tutorial

  1. Pingback: Abstract class in java | Free Courses Hub

  2. Pingback: Object-Oriented Programming in JAVA | Free Courses Hub

Leave a Reply

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