Class vs Object
One of the big questions about OOP is what is a class, vs what is an object.
A class, sometimes called class definition, tells us what the object will look like. What internal variables they are using, and what methods will be used to access and control those values.
However, it is like a blueprint. It does not actually store any information. We (developers/programmers) will build this design/blueprint/recipe.
When we create an object, we take that blueprint, and instantiate it. That is we create one from the design of the class, and start filling it with data.
Objects are created from a class, once the main program is running. Your program can keep creating objects from a class. For example, you might have a class called student, and then have an object for each student in a given course. Each one of those objects is completely unique and kept independent of one another.
Class Variables
Class variables are sometimes called attributes. They help describe the class by giving us ideas of what could be different.
For example, a person class might have attributes of hair_color, height, weight, date_of_birth, etc. Each of these attributes help describe the person. And while some objects might have the same value for an attribute as another, the chances of them all being the same are very low for any well defined class.
These class variables might be simple data types like string, integers, and Booleans. They could be complex like a list or dictionary. You can even have other user defined classes.
Methods
Methods are functions built into a class. They are used to allow access to the variables stored within the object so that program code outside of the class can access them.
Methods can provide protection of our data, making sure only those who have rights to it can access it, and making sure that the data is correct (built in error checking).
Getters and Setters
Two common types of methods are called getters and setters.
Getters are methods used to get the value of a variable in our object. Typically it is named get_<variable name>() in our code, and return a value.
Setters are methods used to set the value of a variable in our object. Typically it is named set_<variable name>(<new value>) in our code. Sometimes those methods have some error detection in them, for example making sure a number is positive if it has to be.
The Three Tenants of OOP
Object oriented programming is based off of three main tenants, besides being grounding in how the data looks.
Encapsulation – the idea that we protect and contain the data for our object. By limiting access to the data and methods we can protect our data from being changed by outside sources.
Inheritance – This is the idea that you can have a class, which is based off of another class. When a class inherits from a base class, it gets a copy of all of the existing variables and methods. This provides a very fast way to create several classes which have related, or common fields and methods.
Polymorphism – allows for the same name in a method to be defined multiple times, usually via function overloading.
An Introduction to Classes was originally found on Access 2 Learn