Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I started reading Concepts of Modern Mathematics and naturally, I came across set theory. I was wondering if someone could clarify my understanding for subsets by way of inheritance in Java or any other language.

If I say that $\mathbb K \subset \mathbb R$ and $\mathbb K$ is some set then would that be the same thing as saying that class A extends B within the context of Java code? Or in other words, $\mathbb K$ inherits the properties of $\mathbb R$ and furthermore, becomes the "child" of $\mathbb R$ or the $\subset$ (subset) of $\mathbb R$?

Would I be correct in my analogy?


share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

The inheritance hierarchy is a bad model for sets and subsets. The main problem is that subsets deal only with single relation, which is containment $\subset$, where inheritance has a two roles:

  • subtyping polymorphism,
  • modeling abilities of objects.

Consider the infamous example of squares and rectangles. In math, of course, the set of all squares is a subset of all rectangles. This may lead to an impression that square should be a subclass of the rectangle (as, the most general Object class it at the very top of the hierarchy). However, this is wrong, because square does not have all the abilities that rectangle has (e.g. having sides of different lengths). However, the other direction, that is the rectangle is a subclass of the square, also has drawbacks, e.g. one could imagine such code

double squareArea(Square s) {
  return s.side*s.side;

which is clearly wrong if the square is a superclass of the rectangle. Some think that this code is wrong, other that it makes the OP paradigm broken, and those forced to use OP frequently use the ad-hoc solution (naturally, there are exceptions) being that the abilities-modeling is more important (to the point that the author of the original Java class hierarchy admits he did it wrong, unfortunately I could not find the citation, so take it with a pinch of salt). One way or another (I don't want to start another Xmas vs. Easter discussion), inheritance is a not a good example of sets and subsets.

If you have to go with Java, there is an interface java.util.Set which would be more helpful that inheritance hierarchy. Indeed, $A \subset B$ if and only if for all $x \in A$ we have $x \in B$, or in java terms

boolean subset(Collection<?> a, Collection<?> b) {
  for (Object elem : b)
    if (!a.contains(b)) 
      return false;
  return true;

Just try of interpreting what are you reading in terms of collections, as those were designed to model sets, multisets, sequences, and so on.

I hope I didn't confuse you ;-)

share|improve this answer
Great counter example! –  gekkostate Feb 20 '13 at 11:49
+1 for making hard concept clear and simple –  Arjang Feb 20 '13 at 11:57
What you're referring to with your example is, that this inheritance hierarchy violates the Liskov substitution principle (see objectmentor.com/resources/articles/lsp.pdf). This problem arises only for mutable objects, which is never the case in mathematics. If rectangles and squares were considered mutable in mathematics, the squares wouldn't form a subset of the rectangles. OTOH if your square and rectangle classes are immutable, it's perfectly ok with respect to the LSP when a square is derived from ("is a") rectangle. –  Elmar Zander Feb 20 '13 at 13:50
@gekkostate Sure. The problem with inheritance I described is a well-known issue, and Barbara Liskov proposed LSP, which helps you avoid it. An example in my post violates LSP and that is why it came out so awkward, the problem is that rectangle method "change one side" that square has to implement might break square constrains. As Elmar Zander pointed out, this could not have happened if the square was immutable (unless your invariants depends on something outside square, but that's a very bad design). Cont. –  dtldarek Feb 21 '13 at 7:35
Cont. In math, your whole world is immutable, so this indeed couldn't have happened, so yes, with some assumptions and constrains, there is a way do describe exactly what you wanted in the original question (i.e. sets and subsets). On the other hand, types are more complicated concepts than sets, and such solution would cripple your (future) intuitions. For example, with parametric polymorphism you could have $\mathtt{Object}$ type and $(\forall \alpha.\ \alpha)$ type, both of them aspiring to be "the most general type". The subtyping relation is much more subtle than just $\subset$. –  dtldarek Feb 21 '13 at 7:48
show 2 more comments

Yes, you can represent these subset relations also as type relations in computer languages. However, Java is not exactly a role model for this. Better look at functional languages, like e.g. Lisp or Scheme, where this exactly modelled as so-called "numerical tower" (see e.g. https://en.wikipedia.org/wiki/Numerical_tower).

In Scheme or Common Lisp you have e.g. that an integer is a rational is a real, modelling exactly this $\mathbb Z\subset \mathbb Q\subset \mathbb R$ subset relation (see e.g. http://en.wikibooks.org/wiki/Common_Lisp/Advanced_topics/Numbers_.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.