- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we define an interface inside a Java class?
Yes, you can define an interface inside a class and it is known as a nested interface. You can’t access a nested interface directly; you need to access (implement) the nested interface using the inner class or by using the name of the class holding this nested interface.
Example
public class Sample { interface myInterface { void demo(); } class Inner implements myInterface { public void demo() { System.out.println("Welcome to Tutorialspoint"); } } public static void main(String args[]) { Inner obj = new Sample().new Inner(); obj.demo(); } }
Output
Welcome to Tutorialspoint
You can also access the nested interface using the class name as −
Example
class Test { interface myInterface { void demo(); } } public class Sample implements Test.myInterface { public void demo() { System.out.println("Hello welcome to tutorialspoint"); } public static void main(String args[]) { Sample obj = new Sample(); obj.demo(); } }
- Related Articles
- Can we define a class inside a Java interface?
- Can we define constructor inside an interface in java?
- Can we define an enum inside a class in Java?
- Can we define an enum inside a method in Java?
- How to write a class inside an interface in Java?
- How to write/declare an interface inside a class in Java?
- Can we define a parameterized constructor in an abstract class in Java?
- Can we define an abstract class without abstract method in java?
- Why can't we define a static method in a Java interface?
- Can we define an abstract class with no abstract methods in Java?
- Can we declare an interface with in another interface in java?
- What happens if we define a concrete method in an interface in java?
- Can we create an object for an interface in java?
- Can we define a method name same as class name in Java?
- Can we declare an interface as final in java?

Advertisements