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?
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
And in another file
Another option would be
and then access the class with MyInterface.MyClass (see
java.awt.geom.Pointfor an example of this sort of structure)