05.X. Object oriented programming in a nutshell
Abel Vertesy
Recap: Data structures in R
Motivation for “Objects”
- Real “items” are more complext than just a dataframe:
- Think of designing an informatics system for a hospital
- You have many patients
- Patients have a lot of attributes
- Name, Height, Weight → Can store it in a row of a data frame
- Medication history, CT-scans → Cannot store it in a row of a data frame
- → A new way of storing is needed an: object
- In a very simple way, you can think of it as a list on steroids.
Patient is class
of object with concrete instances
, like poor John
- Class is a type of object: Think of
patients
and doctors
in the hospital.
- Instance is a concrete example
Calculate BMI from 2 other attributes
BMI is a new attribute of “patient”
Motivation for “Object oriented programming”
- If you always handle the same kind of objects, you can design the functions to work specifically with those objects!
- Write a simple function for BMI:
calc_BMI()
- But you know that all patients have these attribute, so you could write a simple function
- This function can put back the BMI immediately to the object
Patient
.
- These functions for object are called methods.
Methods are functions designed to work with a type of object.
- A method is a function designed for a particular type of object.
- Most Seq-data analysis packages work with R
- Imagine a function like
Cell_quality(SingleCellObject)
Cluster_SC(SingleCellObject)
Take home messages for object oriented programming
- Life is too complex for a data frame → invention of objects.
- Type of object is called class.
- An example of that class is called instance.
- Writing functions to work with objects, called methods.