In what ways are subtypes different from subclasses in usage?

In general, subclassing means to inherit the attributes of a parent. Subtyping merely means that operations on the supertype can be performed on the subtype. Note that subclassing is a special case of subtyping.

in Java, interfaces represent the structure for describing what behaviors a type can exhibit, which makes it the natural representation for subtyping. Subclassing is manifested in the class hierarchy.


In Java, subclassing is a kind of subtyping.

There are a number of ways Java allows subtyping:

  1. When class A extends B, A is a subtype of B because B b = new A(...); is ok.
  2. When interface A extends B, A is a subtype of B because B b = new A() { ... } is ok.
  3. When class A extends B, A[] is a subtype of B[] because B[] b = new A[0] is ok.
  4. When class A implements B, A is a subtype of B because B b = new A(...) is ok.

It sounds like you want a way to distinguish one from the others. The below should do that.

static boolean isSubclass(Class<?> a, Class<?> b) {
  return !b.isArray() && !b.isInterface() && b.isAssignableFrom(a);
}

It won't handle subtyping of generic classes due to type erasure though. Class instances don't carry type parameters at runtime so there is no way to distinguish the runtime type of a new ArrayList<String>() from a new ArrayList<Integer>().