File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Why Class in an Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why Class in an Interface" Watch "Why Class in an Interface" New topic

Why Class in an Interface

Md. Mohd

Joined: Apr 11, 2006
Posts: 12
Dear Rancher's

"We can define a public class in an Interface". For example

public interface MyInterface {

public class MyClass {
public void print(String message) {


Now the questions are :
1. What benefit we get by defining the class in an Interface.
2. Since only the public classes can be defined in an interface, how these classes are different from nornmal public classes.
3. When to use this approach of defining a class in an interface

Please guide me with your views

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

It's a static nested class, the same as you could define inside any other class. If you had a very simple class that was used only as an argument to one of the methods in an interface, you might use a class inside that interface to define it. This is not the kind of thing you ever need to use -- but sometimes it might be a convenient way to express the relationship between a class and an interface.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Why Class in an Interface
It's not a secret anymore!