简体   繁体   中英

Java implement class in interface

Say I have the following classes:

public abstract class Crop {
}

public abstract class Fruit extends Crop {
}

public interface Edible /* ALWAYS IMPLEMENTED BY A CROP */ {
}

public class Apple extends Fruit implements Edible /* BOTH A CROP AND EDIBLE */ {
}

public class Holly extends Fruit /* NOT EDIBLE */ {
}

public class Wheat extends Crop implements Edible {
}

Now, I want to make sure that every Edible (interface) is a Crop (class).

Why can I not say:

public interface Edible implements Crop {
}

So that Edible itself can only be implemented by classes that extend Crop ?

Is there a workaround for this, especially when passing Edible as a generic parameter that requires a Crop ?

The only option to restrict what classes can implement an interface is using a sealed interface (introduced in Java 17), which restrict what class can inherit it. However, your use-case has the "problem" that not all crops are edible, which complicates matters.

On the face of it, your problem is not solvable, unless you want to explicitly list all possible classes in the interface.

For example, your problem could be solved like

public sealed interface Edible 
        permits Apple, Wheat {
}

public abstract class Crop {
}

public abstract class Fruit extends Crop {
}

public final class Apple extends Fruit implements Edible /* BOTH A CROP AND EDIBLE */{
}

public class Holly extends Fruit /* NOT EDIBLE */ {
}

public final class Wheat extends Crop implements Edible {
}

You could relax some of the constraints by using something like:

public sealed interface Edible 
        permits EdibleFruit, Wheat {
}

public abstract class Crop {
}

public abstract class Fruit extends Crop {
}

public non-sealed abstract class EdibleFruit extends Fruit implements Edible {
}

public class Apple extends EdibleFruit /* BOTH A CROP AND EDIBLE */{
}

public class Holly extends Fruit /* NOT EDIBLE */ {
}

public final class Wheat extends Crop implements Edible {
}

But that could lead to a complicated hierarchy (eg consider that some sub-types of an edible fruit are not edible).

It might make more sense to handle this differently, eg a check on edibility or toxicity, or maximum safe dose, or something like that.

You cannot implement a Java Class on an Interface. Interfaces can only extend other interfaces. So, in that way, you can declare another interface (and implement it on Crop ). Then, you can extend this new interface on Editable .

This can be something like:

public abstract class Crop implements NewInterface{
}

public interface Edible extends NewInterface {
}

Why not constraint Edible ?

interface Edible<T extends Crop> {}

so that every edible must instanciate the interface if it subclass Crop :

class A extends Crop implements Edible<A> {} // Ok
class B implements Edible<B> {} // Bad

At least every Edible needs to be instantiated with a class that is a Crop . But you can do weird things like:

class C implements Edible<A> {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM