Is there a way to implement algebraic types in Java?
Asked Answered
T

4

7

Is it possible, in Java, to enforce that a class have a specific set of subclasses and no others? For example:

public abstract class A {}
public final class B extends A {}
public final class C extends A {}
public final class D extends A {}

Can I somehow enforce that no other subclasses of A can ever be created?

Terminus answered 4/10, 2008 at 3:14 Comment(0)
T
0

Church encoding to the rescue:

public abstract class A {
  public abstract <R> R fold(R b, R c, R d);
}

There are only three implementations possible:

public final class B extends A {
  public <R> R fold(R b, R c, R d) {
    return b;
  }
}

public final class C extends A {
  public <R> R fold(R b, R c, R d) {
    return c;
  }
}

public final class D extends A {
  public <R> R fold(R b, R c, R d) {
    return d;
  }
}
Terminus answered 24/6, 2019 at 13:47 Comment(0)
P
4

Give class A a constructor with package-level accessibility (and no other constructors).

Thanks, Dave L., for the bit about no other constructors.

Pauli answered 4/10, 2008 at 3:17 Comment(3)
Alternatively all as nested classes with A having a private constructor only.Provencher
import static to the rescue, marxidadTerminus
Wouldn't anyone be able to copy the package name, and put their own class in that package that extends A?Faustino
E
3

You probably want an enum (Java >= 1.5). An enum type can have a set of fixed values. And it has all the goodies of a class: they can have fields and properties, and can make them implement an interface. An enum cannot be extended.

Example:

enum A {

  B,
  C,
  D;

  public int someField;

  public void someMethod() {
  }


}
Ellersick answered 4/10, 2008 at 3:21 Comment(3)
If I'm not mistaken, in your example, B, C, and D are values, not types.Terminus
But it is good if you don't want the subtypes to have state (in the example they don't even implement any methods).Provencher
Tom, my mouse and keyboard hang, once PC resumes from sleep. You may think it is bad -- you cannot control your computer. But it is good if you do not want to! I always liked this kind of logic. Internet is blocked? No problem, you provider defends you from viruses! It would be worthwile if Java would not exist at all (blank grammar). It would prevent you from writing any program. But you may want to. See? Limitation gives an advantage!Audra
E
0

You could put class A,B,C,D in a seperate package and make class A not public.

Elastomer answered 4/10, 2008 at 3:18 Comment(1)
Making class A private sort of defeats the purpose of algebraic datatypes (i.e., polymorphism)Pauli
T
0

Church encoding to the rescue:

public abstract class A {
  public abstract <R> R fold(R b, R c, R d);
}

There are only three implementations possible:

public final class B extends A {
  public <R> R fold(R b, R c, R d) {
    return b;
  }
}

public final class C extends A {
  public <R> R fold(R b, R c, R d) {
    return c;
  }
}

public final class D extends A {
  public <R> R fold(R b, R c, R d) {
    return d;
  }
}
Terminus answered 24/6, 2019 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.