Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 2,991,796 members, 7,286,869 topics. Date: Thursday, 02 February 2023 at 08:50 AM

Defining OOP In Terms Of Set Theory - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Defining OOP In Terms Of Set Theory (1861 Views)

Why Is OOP PHP So Confusing? / A Beginners Guide To Object-oriented Programming (OOP) In PHP 5+ / Help ! Sql Defining Relationship Between Tables (2) (3) (4)

(1) (Reply)

Defining OOP In Terms Of Set Theory by kambo(m): 1:07am On Sep 26, 2014
Turns out typical oop (object oriented programming ) with its nested inheritance structures can
be neatly defined using high school set theory knowledge.
some definitions:
A set is a mathematical ideology for a group.
A group of things. Not to be confused with the same mathematical object , group , in group theory.
In real i.e physical life, we have no problem with the idea of a group.
like a fridge - for holding food. A school bag for holding books and stationery,
a wallet for holding money etc. An office for holding staff,machines,puters,and bosses.
operations on sets include - query for membership.
in the set of alphabets - a,b,c,e....z. the number 1 isnt a member of that set.
2.) intersection
two sets intersect to create another set.
the result of an intersection is a set , which contains elements that both sets have in common.
if A = letters in the word fish
B = letters in the word meat
A intersection B = null/empty set. cuz fish and meat have no letters in common.

3.) Union,
union is analogous to mathematical addition.
with a union the combined sets create a larger resultant set.
A = letters in the word fish
B = letters in the word meat
A union B = fishmeat.

Application to oop.
Memberships and inheritance . Relationships can be modelled using set theory.

lets say Ade is a father and he works as a teacher in a secondary school and is married to Ada.

oop constructs would be:

class Father{

public abstract int number of children();

class/interface Employee{
public string nameOfWorkPlace();
public string nameOfOccupation();

class Man{
public String Name();
public Sex Sex(){return Sex.MALE;}

Father extends Man{}

A problem quickly arises.
Many abstract classes and interfaces have to b created to model the relationship
like a human class has to be super class from which male and female sub classes are derived
Employee has to be made an interface to work around single inheritance problems etc

it goes on and on ...

With set theory - it gets flatter and easier.
Father = set of all fathers.
Employee = set of all employees
Male = set of all men
Wife = set of all wives
Person = an object that is a base element in the sets.
A person can be amember of more than 1 set . Logical.
so lets create the person object and register it with its respective set.
Person adeP = new Person("Ade",M);
lets create the fathers set. Fathers = Set.createSet("Fathers"wink;
is ade a father?
Fathers.isMember(adeP) = if it returns affirmative - you gat your answer.
in oop , the solution is clumsy - traversing the inheritance tree!!- yikes. hyper ugly.

is ade an employee?

is ade a Husband?

lets say - ade gets fired from his teaching job.
what happens - he exits the set of employees and joins the set of JobSeekers.
Employees.disMember(adeP) .


Ade dies . he leaves the set of living things . joins set of deceased.

More work...
in a set theoretic language. Objects would be broken into two broad classes. Sets, elements.
A set can contain sets.
all the contents in a set are elements.
elements can tell what sets they belong to.
Ade /person object could be queried directly to determine what sets it belongs to.
Person.getBelongingSets(); // returns a Set of names of sets it is members of.

Actually using this approach - one thing stands out.
Multiple inheritance (thinking c++) here is more natural and reflective of real life relationships than
single inheritance. With single inheritance mechanism of mainstream oop, lots of scaffolding has to be
written to model relationships properly and even with that it just doesnt work right . Think
interfaces,abstract classes,etc.

(1) (Reply)

Learn How To Build Websites With Java EE / Can I Program A Java Application Or Game Using A Java Phone? / Python: Should I Use Tkinter Or Pyqt?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2023 Oluwaseun Osewa. All rights reserved. See How To Advertise. 52
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.