This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why Class in an Interface" Watch "Why Class in an Interface" New topic
Author

Why Class in an Interface

Md. Mohd
Greenhorn

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) {
System.out.println(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

Regards
Mohd
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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: http://aspose.com/file-tools
 
subject: Why Class in an Interface
 
Similar Threads
Java's Inner Classes - Does those Help or is better Avoid those?
Marker Interfaces in Java
Question on implentation inheritance
Can we define a inner class in an interface?
interfaces Vs abstract classes