top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

Interface in Java

Mar 26, 2025
2 min read

Collection of abstract methods in java is called Interface.  The interface is used to achieve abstraction in which you can define methods without their implementations. 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.


Declaring and Implementing an Interface in Java


The interface keyword is used to declare an interface and Implements keyword is used to implement an interface.  The implements keyword appears in the class declaration following the extends portion of the declaration.


Example for Declaration of a Java Interface

Declaration of a Java Interface
Declaration of a Java Interface

Example for Implementing a java Interface

  Implementing a java Interface


Some of the key points on Interface.


1. Abstract Methods: An interface can contain abstract methods (methods without a body). Any class that implements the interface must provide concrete implementations for these methods.

Example:

 

2. Default Methods: Interfaces can also have default methods with a body. These methods have a default implementation and can be optionally overridden by implementing classes.

Example:


3. Static Methods: Java interfaces can also have static methods. These methods belong to the interface and cannot be overridden by implementing classes.

Example:


4. Multiple Inheritance: A class can implement multiple interfaces, which is Java's way of providing multiple inheritance.

Example:


5.  No Constructors: Interfaces cannot have constructors because they cannot be instantiated on their own. You can only instantiate a class that implements the interface.

6.  Implementing an Interface: A class uses the implements keyword to implement an interface. If a class implements an interface, it must provide implementations for all the abstract methods of the interface, unless the class is abstract.

Example:

 

Why do we need interface in Java?

·       Java interfaces define method signatures without implementations, giving classes a template to follow.  

·       It's a way to achieve multiple inheritance, as a class can implement multiple interfaces.

·       They encourage code flexibility, which facilitates scalability and easier maintenance.

 

Abstract class vs Interface 

Abstract Class

Interface

Can have both abstract and concrete methods

Can have abstract methods, default and static methods.

Can have final, non-final, static, and non-static variables.

Only static and final variables.

Can extend only one class (abstract or not)

A class can implement multiple interfaces

Can have constructors

Cannot have constructors

Can provide the implementation of interface methods

Cannot provide the implementation of abstract class methods

 

 
 

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page