Interface In Java

In the previous tutorial, we learned about the Abstract Class and Abstract Methods in Java.. In this tutorial, we will learn about interface in java.

214
Interface In Java
Interface In Java

Java provides a new feature which is called the interface. This function contains the method declarations but not their implementation part.

As we have previously learned that we use the ‘class’ keyword before creating a class in Java. Similarly, in Interface, we do not use this class keyword but use the ‘interface’ keyword.

Example –

interface Name_of_interface{
int x;
void someFunction();
}

The fields in the Interface are by default–

  • Public
  • Static
  • Final
  • Abstract

But, if there is just the declaration part in Interface, then how are we going to use it?

Implementing Interface in Java

The interface is implemented just as we used to create the sub-class of any class. But, while creating the sub-class, we used to write the Extend keyword, but here we are going to write the implements keyword.

We will understand it better with the help of the example given below –

interface a{
void function_name();}
class A implements i1{
public void function_name{
// coding part
}
}

 Now, we will learn some more points regarding interface –

  • An interface like that of an abstract class cannot be instantiated.
  • Interfaces do not have constructors.
  • If a class that implements an interface does not define all the methods of the interface, then it must be declared abstract and method definitions should be provided by the subclass that extends the abstract class.
  • As we have learned that in Java, multiple inheritances is not possible. But, multiple inheritances is definitely provided by Java.

We will learn this, with the help of the example given below –

interface i1{
// coding part of i1
}
interface i2{
// coding part of i2
}
interface i3 extends i1,i2
{//coding parts of i3}
class A {
}
class B extends A implements i3{
}

 

Object Reference

  • You cannot create an object of any reference we can create an object reference
  • Object reference of the interface can refer to its subclass type too.

So, we have learned about the Interface in Java. While it is quite similar to Abstract class, because abstract class too contains the functions which we need to re-define in the sub-class, and we cannot instantiate them but there are many differences too between them. Mentioned below are some of the differences between Interface and Abstract class in Java –

  • An abstract class is a ‘class’, but the interface is not a cl
    ass.
  • An abstract class can have any access modifiers for members but, the interface can have only public members.

Example of Abstract class –

abstract class student{
public String name;
public void setName(String n){name = n;}
public String getName(){return(name);}
}

Example of Interface –

{
double PI = 3.1428;
int add(int x,int y);
int multiply(int x,int y);
}
  • An abstract class may or may not contain abstract functions. The interface cannot have methods defined within them.

Example –

abstract class student{
public String name;
public void setName(String n){name = n;}
public String getName(){return(name);}
}

In this, abstract class does not have any abstract class defined within it.

Example –

{
double PI = 3.1428;
int add(int x,int y);
int sub(int x,int y);
}
  • Abstract class can have static or non-static members. The interface can have only static member variables.
  • In abstract class, we can have final or non-final members. But, the interface can have only final member variables.
  • The interface cannot have any constructor, but the abstract class contains the constructor

Try these quizzes:

Previous QuizAbstract Class and Abstract Methods in Java
Next QuizDifferent Types of Control Flow Statement in Python

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.