This week's book giveaway is in the JDBC forum.
We're giving away four copies of Make it so: Java DB Connections & Transactions and have Marcho Behler on-line!
See this thread for details.
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

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
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: 24195

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
jQuery in Action, 3rd edition