Pablo
Pablo

Reputation: 3467

Declaring Interface inside class file in Java

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?

Upvotes: 1

Views: 17918

Answers (6)

sahil
sahil

Reputation: 1

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();
   }
}

Upvotes: 0

pb2q
pb2q

Reputation: 59617

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.

Upvotes: 1

James
James

Reputation: 2542

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)

Upvotes: 12

Sled
Sled

Reputation: 18949

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?

Upvotes: 0

Alex Coleman
Alex Coleman

Reputation: 7326

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.

Upvotes: 2

Code-Apprentice
Code-Apprentice

Reputation: 83527

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.

Upvotes: 0

Related Questions