What is the difference between Set, and Class in object oriented languages?

1.8k Views Asked by At

What is the difference between Set in set theory, and Class in object oriented languages?

2

There are 2 best solutions below

2
On

There are debates about the definition, but, for elementary work, a set is just a collection of things, which are called its "members". So $\{3, \pi, \text{Jupiter}\}$ is a set whose members are two numbers and a planet. The members of a set do not all have to be the same kinds of things, and they do not need to have any relationship to each other.

In object-oriented programming, a class provides a "blueprint" or "template" for objects of a similar type, that have some common set of properties and methods. Specific objects that follow the blueprint are called "instances" of the class.

In programming, the closest thing to a set is a C# ArrayList or a Python tuple. These are both collections whose members can be anything you like. For example

ArrayList a = { 3, System.Math.Pi, "hello" };

is a perfectly legal definition of an ArrayList whose members are two numbers and a string.

But sets and ArrayList-like collections still have some large differences. ArrayLists are finite, and sets are often infinite. Also, the elements of an ArrayList have a specific order, and elements of a set do not. In common programming languages, I can't think of a data structure that represents an unordered collection.

In short, sets and classes have very little in common, so asking how they are different is a somewhat odd question.

1
On

In set theory, there are things called equivalence relations which can be used to glob things that are alike into sets called equivalence classes. I think this is where the class idea comes from programming. The objects created by a class in programming all have the same structure and so in some sense they are equivalent!

A class in programming is a "program-code-template" so I don't know what type of "thing" a template even is. This question is way too hard.