Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Class in an Interface

 
Md. Mohd
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic