DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Projections/DTOs in Spring Data R2DBC
  • Functional Interfaces in Java
  • Functional Interface Explained in Detail Introduced From Java 8
  • Smart Dependency Injection With Spring: Assignability (Part 2 of 3)

Trending

  • Modular Software Architecture: Advantages and Disadvantages of Using Monolith, Microservices and Modular Monolith
  • Choreography Pattern: Optimizing Communication in Distributed Systems
  • A Better Web3 Experience: Account Abstraction From Flow (Part 2)
  • Best Practices for Writing Clean Java Code
  1. DZone
  2. Coding
  3. Java
  4. What is an Inner Interface in Java?

What is an Inner Interface in Java?

Ryan Wang user avatar by
Ryan Wang
·
Aug. 28, 13 · Interview
Like (2)
Save
Tweet
Share
21.57K Views

Join the DZone community and get the full member experience.

Join For Free

inner interface is also called nested interface, which means declare an interface inside of another interface. for example, the entry interface is declared in the map interface.

public interface map {
	interface entry{
		int getkey();
	}
 
	void clear();
}

why use inner interface?

there are several compelling reasons for using inner interface:

  • it is a way of logically grouping interfaces that are only used in one place.
  • it increases encapsulation.
  • nested interfaces can lead to more readable and maintainable code.

one example of inner interface used in java standard library is java.util.map and java.util.map.entry. here java.util.map is used also as a namespace. entry does not belong to the global scope, which means there are many other entities that are entries and are not necessary map’s entries. this indicates that entry represents entries related to the map.

how inner interface works?

to figure out how inner interface works, we can compare it with nested classes. nested classes can be considered as a regular method declared in outer class. since a method can be declared as static or non-static, similarly nested classes can be static and non-static. static class is like a static method, it can only access outer class members through objects. non-static class can access any member of the outer class.

java-nested-classes

because an interface can not be instantiated, the inner interface only makes sense if it is static. therefore, by default inter interface is static, no matter you manually add static or not.

a simple example of inner interface?

map.java

public interface map {
	interface entry{
		int getkey();
	}
 
	void clear();
}

mapimpl.java

public class mapimpl implements map {
 
 
	class implentry implements map.entry{
		public int getkey() {
			return 0;
		}		
	}
 
	@override
	public void clear() {
		//clear
	}
}


Interface (computing) Java (programming language)

Published at DZone with permission of Ryan Wang. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Projections/DTOs in Spring Data R2DBC
  • Functional Interfaces in Java
  • Functional Interface Explained in Detail Introduced From Java 8
  • Smart Dependency Injection With Spring: Assignability (Part 2 of 3)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: