Interface in Java with Examples
Interface in Java with Examples
Interface
Java interface is a collection of abstract methods. The interface is used to
achieve abstraction in which you can define methods without their
implementations (without having the body of the methods).
• An interface is a reference type and is similar to the class.
• Along with abstract methods, an interface may also contain
constants, default methods, static methods, and nested types.
• Method bodies exist only for default methods and static methods.
• Writing an interface is similar to writing a class.
• However, a class describes the attributes and behaviors of an object.
• An interface contains behaviors that a class implements.
• Unless the class that implements the interface is abstract, all the
methods of the interface need to be defined in the class.
Differences
However, an interface is different from a class in several ways, including :
• You cannot instantiate an interface.
• An interface does not contain any constructors.
• All of the methods in an interface are abstract.
• An interface cannot contain instance fields. The only fields that
can appear in an interface must be declared both static and
final.
• An interface is not extended by a class; it is implemented by a
class.
• An interface can extend multiple interfaces.
Why do we need Interfaces in java?
We know the concept of multiple inheritances where one class is derived
from more than one superclass. For example a definition like the below.
What is the need for the interface when we already have abstract
classes to define abstract methods?
Java doesn’t support multiple inheritances with classes. So we must use
the interface as a superclass to develop abstraction for supporting
multiple inheritances.
If this is not clear at the moment, then don’t worry we will discuss this in
detail with an example.
Javac Example.java
Example.class
Java Example
Exception in thread “main” java.lang.NoSuchMethodError: main
Interface Example in Java:
Let us see an example to understand how the interface is used in the java
application.
Let us first create an interface with the name Shape and then copy and
paste the following code into it.
interface Shape
{
public double Area();
public double Volume();
}
As you can see, here, we created the interface with two methods (Area
and Volume).
Now we will create two classes (Cube and Circle) and then we will
implement the Shape interface i.e. we will implement the two abstract
methods (Cube and Circle) of the Shape interface as shown in the below
code.
Now in the main method class, we will create an instance and consume
the two methods as shown in the below code.
public class Main
{
public static void main (String[]args)
{
Shape s1 = new Cube ();
System.out.println ("The area of Cube is : " + s1.Area ());
System.out.println ("The volume of Cube is : " + s1.Volume ());
Shape s2 = new Circle ();
System.out.println ("The area of Circle is : " + s2.Area ());
System.out.println ("The volume of Circle is : " + s2.Volume ());
}
}
Output:
Rules for implementing Java Interfaces
interface Sports
{
double weight = 50.0;
void ShowWeight ();
}
Output:
Relationship Between Classes and Interfaces in Java:
What are the similarities between interface and abstract class in Java?
An interface is similar to an abstract class in the following ways
• Both interface and abstract class cannot be instantiated means we
cannot create an object.
• But we can create a reference variable for both interface and an
abstract class.
• The subclass should implement all abstract methods.
• Both cannot be declared final.
Difference Between Interface and Abstract Class in Java:
The main difference is to be answered in the interview is as follows