Java - Can an interface define a class?

snotnose

Ars Praefectus
3,453
Subscriptor
I have an interface that returns a class containing common classes. But where do I define the class? What I want is something like:
Code:
public interface Foo {
    String getName();
    FooType getFoo(int a, float b);

    class FooType {
        Color a;
        Complex b;
        int c;
    }
}
Obviously that doesn't compile, but I hate to make a file FooType.java with just the 5 lines above in it.

How do I do what I want to do?
 

OrangeCream

Ars Legatus Legionis
56,742
I have an interface that returns a class containing common classes. But where do I define the class? What I want is something like:
Code:
public interface Foo {
    String getName();
    FooType getFoo(int a, float b);

    class FooType {
        Color a;
        Complex b;
        int c;
    }
}
Obviously that doesn't compile, but I hate to make a file FooType.java with just the 5 lines above in it.

How do I do what I want to do?
Not a Java expert but why can’t you define the interface with getFoo and then in the next line after the interface define the implementation? Is the concern that you want to group the interface and implementation with other code?

Like this? Where the CurrencyFactory interface is Foo and getCurrency is getFoo?
https://www.geeksforgeeks.org/factory-method-java-design-patterns/
I’m not sure what the issue is.
 
D

Deleted member 43669

Guest
See https://docs.oracle.com/javase/specs/jls/se21/html/jls-9.html#jls-9.5 , but don't do it.

Nesting classes and interfaces seems cute, and creating extra files seems a burden, but nesting is very uncommon- IIRC, no one else will read your code, so well, you won't be confusing anyone, which is the major argument not to do this. However, the rules for nesting are a bit complex and you won't find a ton of accurate docs, so it's best to stay in the beaten path.

Edit: if your editor/environment is not comfortable with multiple files, you likely need a better one.

Edit 2: Are you sure you need an interface?
 

snotnose

Ars Praefectus
3,453
Subscriptor
Because then javac says "warning: auxiliary class FooType in ./iface.java should not be accessed from outside its own source file". Which I interpret as "guess not".

I'm learning Java and OO by writing a Mandelbrot program. One of my test routines is:
Code:
        List <ColorScheme> schemes = new ArrayList<>();
        schemes.add(new col_p1());
        schemes.add(new col_p2());
        schemes.add(new RgbCalculator());
        schemes.add(new wavelengthToRGB());
        schemes.add(new WavelengthToRGB2());

        int barStart = 0;
        int barSize  = 90;
        int barInc   = 100;
        for(int i = 0; i <= size; i++) {
            barStart = 0;

            for(ColorScheme cs : schemes) {
                g.setColor(cs.getRGB(i, size));
                g.drawLine(i, barStart, i, barStart + barSize);
                barStart += barInc;
            }
        }

Each scheme implements Color getRGB(int value, int maxValue), where 0 <= value < maxValue. Except now I need getRGB() to return another object as well as a Color and I don't know how best to do that.
 
Last edited:
D

Deleted member 43669

Guest
The need for public is one of the reasons I don't recommend doing nested classes/interfaces in general- they just add some complexity.

However, returning multiple values from functions is one of the few situations where they might be advisable. (I think I'd play with defining a nested public record on the class with methods which must return multiple values. Or use a tuples library.)