0% found this document useful (0 votes)
39 views5 pages

Interface in Java

A Java interface is a blueprint of a class that specifies its behavior through abstract methods. An interface contains only abstract methods and static constants, while classes implement interfaces. Interfaces achieve abstraction and multiple inheritance in Java. There are two types of interfaces: functional interfaces with one abstract method and marker interfaces with no methods.

Uploaded by

yuvikatuteja4
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
39 views5 pages

Interface in Java

A Java interface is a blueprint of a class that specifies its behavior through abstract methods. An interface contains only abstract methods and static constants, while classes implement interfaces. Interfaces achieve abstraction and multiple inheritance in Java. There are two types of interfaces: functional interfaces with one abstract method and marker interfaces with no methods.

Uploaded by

yuvikatuteja4
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

What is Interface in Java?

A Java interface is like a blueprint of a class. It is an abstract type that specifies the behaviour
of a class. Java interfaces contain static constants and abstract methods. In an interface in
Java programming language, there can only be abstract methods and no method body. It is a
mechanism used to achieve abstraction and multiple inheritance.

In simple words, we can say that an interface program in Java can have variables and abstract
methods, but it can’t have a method body.

Important Things to Know About Java Interfaces


A few key points to remember about an interface in Java:

 An interface mentions what a class must do rather than how. Hence, it is known as a
blueprint of the behaviour of a class.
 We identify a type of entity by its behaviour and not its attribute; we define it as an
interface.
 Behaviour is represented as an interface unless each sub-type of a class has that
behaviour.
 It represents the Is-A relationship.
 It doesn’t have a constructor.
 Similar to a class, an interface can have variables and methods. However, methods
declared in an interface are abstract by default.
 Since Java 8, an interface has default and static methods.
 If a class implements an interface without providing method bodies for all specified
functions, it is declared as an abstract.
 Since Java 9, an interface can have private methods.

Java Interface Example


Here's the example program for interface in Java

// Example interface
interface Shape {
double calculateArea();
double calculatePerimeter();
}
// Implementing the interface
class Rectangle implements Shape {
private double length;\
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override\
public double calculateArea() {
return length * width;
}
@Override
public double calculatePerimeter() {
return 2 * (length + width);
}
}
// Using the interface
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5, 3);
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("Perimeter: " + rectangle.calculatePerimeter());
}
}

Explanation:

 In this example, the Shape interface declares two methods: calculateArea() and
calculatePerimeter(). The Rectangle class implements the Shape interface and
provides the implementation for these methods.
 In the Main class, we create an instance of Rectangle and call the calculateArea() and
calculatePerimeter() methods to calculate and display the area and perimeter of the
rectangle.

Types of Java Interfaces


There are two types of interfaces in Java:

1. Functional Interface

The functional interface has exactly one abstract method but can contain any number of static
and default methods. It represents a single unit of the behaviour of a class that is used as a
method reference or lambda expression.

Functional interfaces are also known as SAM interfaces. To make an interface with a single
abstract method as a functional interface, annotate it with @FunctionalInterface.

Examples of a Functional Interface include:

 Runnable: Includes only one run() method.


 ActionListener: Includes one actionPerformed().
 ItemListener: Has one itemStateChanged() method.

2. Marker Interface

A marker interface doesn’t contain any methods, abstract methods, fields, or constants. It is
an empty interface that indicates a certain behaviour a class implements.

Examples of a Marker Interface are

 Cloneable Interface: It belongs to java.lang packages and generates a copy of an


object with a different name.
 Serializable Interface: It belongs to the java.io package and is used when you want
to make a serializable class. Serialization means a mechanism in which the object
state is ready from memory and is written from the database or into a file. When you
implement this interface, it serializes or deserializes the object’s state of the class.
 Remote Interface: It belongs to the java.rmi package and is used to find interfaces whose
methods are invoked from a non-local virtual machine. If you want to mark objects as
remote, you need to implement a remote interface. The object can be accessed from the
host of another machine.

Multiple Interface in Java


Multiple interfaces in Java refer to a situation where a class can implement more than one
interface. This allows a class to inherit the methods and constants from multiple interfaces,
enabling it to fulfill multiple contracts simultaneously.

To implement multiple interfaces, a class follows the syntax:

class MyClass implements Interface1, Interface2, Interface3 {


// Class implementation
}

Example:

Here's an example that demonstrates the concept of multiple interfaces:

interface Flyable {
void fly();
}
interface Swimmable {
void swim();
}
class Bird implements Flyable, Swimmable {
@Override
public void fly() {
System.out.println("Bird is flying.");
}
@Override
public void swim() {
System.out.println("Bird is swimming.");
}
}
public class Main {
public static void main(String[] args) {
Bird bird = new Bird();
bird.fly();
bird.swim();
}
}

Explanation:

In this example, we have two interfaces: Flyable and Swimmable. The Bird class implements
both interfaces, which means it needs to provide an implementation for the fly() method from
the Flyable interface and the swim() method from the Swimmable interface.
In the main() method, we create an instance of Bird and call both the fly() and swim()
methods. Since Bird implements both interfaces, it can perform both flying and swimming
actions.

Difference Between Interface and Class in Java


Although many think a class and an interface have a lot in common, the two concepts are
completely different. Here are a few differences between a class and an interface.

Class Interface

Here, you can create objects and instantiate Here, you can create objects but can’t
variables. instantiate variables.

Keyword used- class. Keyword used- interface.

It stores the entire method definition. It stores just the signature of a method.

It contains concrete methods, including


It can’t have concrete methods.
implementation.

It includes static methods. It doesn’t have static methods.

In a class, access specifiers are essential and In an Interface, access specifiers are not
include private, protected, and public required and only a public specifier is
specifiers. used.

Includes a constructor. No constructor.

Includes data members. Doesn’t include data members.

You might also like