1

in Objective-C we can define a protocol and a implementation in the same header file. For example:

@class GamePickerViewController;

@protocol GamePickerViewControllerDelegate <NSObject>
  - (void)gamePickerViewController:
    (GamePickerViewController *)controller 
    didSelectGame:(NSString *)game;
@end

@interface GamePickerViewController : UITableViewController

@property (nonatomic, weak) id <GamePickerViewControllerDelegate> delegate;
@property (nonatomic, strong) NSString *game;

@end

This way if I include the .h file I will have access to the protocol defined inside the file. I'm looking for a similar structure in Java cause I find it useful in some cases where I would like to avoid creating too many files (interface file+class file). That way I could declare:

public class MyImplementation implements AnotherClass.MyInterface{
      AnotherClass otherClass;
}

I Think nested classes inside interfaces is the way to go. I am correct? or there's nothing similar in Java?

Pablo
  • 3,433
  • 7
  • 44
  • 62
  • Possible duplicate of this one: http://stackoverflow.com/questions/7133497/java-public-interface-and-public-class-in-same-file – Marvo Aug 07 '12 at 17:24
  • 2
    Why are you avoiding creating more files? Java encourages a separate file for each class and interface definition. – Sled Aug 07 '12 at 17:25
  • I agree with ArtB. Further, right or wrong, it breaks the convention most Java programmers use. Future programmers will be a little baffled by this kind of thing. – Marvo Aug 07 '12 at 17:27
  • Actually, this is a common pattern among Android developers nowadays. Take a look at Recycler view adapters and holders, they do something similar to what this question is asking – Pablo Mar 10 '16 at 18:44

6 Answers6

12

You can nest classes, and have the nested class be public static, this allows them to be in the same Source file (although it is unusual, it is more normal to put them together in a package and use seperate source files)

For example this is allowed

public class AnotherClass {

    public static interface MyInterface{
        // Interface code
    }

    public static class MyClass{
        //class code
    }
}

And in another file

public class MyImplementation implements AnotherClass.MyInterface{

}

Another option would be

public interface MyInterface{
    public static class MyClass implements MyInterface{
    }
}

and then access the class with MyInterface.MyClass (see java.awt.geom.Point for an example of this sort of structure)

James
  • 2,483
  • 2
  • 24
  • 31
  • 4
    I disagree that nested classes are unusual. In my opinion, they are not unusual at all and I would say that Java idioms based on nested classes are commonplace. The Builder creational design pattern relies upon a static nested class, for example, and there are many others. Joshua Bloch, in Effective Java 2nd Ed, devotes an item to "favor static nested classes over inner classes". – scottb Jul 13 '13 at 13:07
2

You can nest classes and interfaces like that, and have them be public! However, you can't implement/extend a class/interface where the class extended is nested in the class you want to extend it

So this won't work:

class A extends A.B {
    public class B {

    }
}

It's fine having class B public in there, but the top level class cannot extend an internal class.

Alex Coleman
  • 7,216
  • 1
  • 22
  • 31
1

Using nested classes you can achieve something similar: packaging an implementation along with an interface, e.g.:

public interface MyInterface
{
    public class Implementation implements MyInterface
    {

    }
}

Now you have both MyInterface and a concrete implementation MyInterface.Implementation.

Michael Shaffer
  • 374
  • 2
  • 16
pb2q
  • 58,613
  • 19
  • 146
  • 147
  • That's not entirely true. You can, for instance have a public nested class: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html – Nathaniel Ford Aug 07 '12 at 17:25
0

What you could do is define the interface and then have a default implementation as an anonymous inner class, class static variable.

interface AProtocol {
    String foo();

    static final AProtocol DEFAULT_IMPLEMENTATION = new AProtocol(){
            @Override
            public String foo(){
                return "bar!";
            }
        };
}

Is that what you mean?

Sled
  • 18,541
  • 27
  • 119
  • 168
0

The Java API does this kind of thing quite often with classes. For example JFormattedTextFiled.AbstractFormatter. Notice that the declaration includes the static modifier. I don't see why you couldn't do this with interfaces as well.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0
interface B {
   public void show();

   class b implements B {
      public void show() {
         System.out.println("hello");
      }
   }
}

class A extends B.b {
   public static void main(String ar[]) {
      B.b ob=new B.b();
      ob.show();
   }
}
dan1st
  • 12,568
  • 8
  • 34
  • 67
sahil
  • 1