Objected Oriented Programming (OOP) is a term that will come up often as you start your journey of learning on how to become a software developer.
It is an important concept to understand if you want to continue to improve as a developer.
Here is the definition of OOP from PC Magazine
"A programming language structure wherein the data and their associated processing ("methods") are defined as self-contained entities called "objects." The norm today, object-oriented programming (OOP) languages, such as C++ and Java, provide a formal set of rules for creating and managing objects. The data are stored in a traditional relational database or in an object database if the data have a complex structure. See O-R mapping and object database."
Link to PC MAG Encyclopedia
In order to getting a better understanding of OOP, we will need to know what these important concepts are:
I will be using Ruby to demonstrate the concepts of OOP.
What is a Class?
A class can be thought of as a blueprint to create objects. Classes can contain methods, variables or even constants.
Let's see how this works with an example.
Class Mammal end
For the remainder of this series on what is OOP, we will continue to use the Mammal class for our examples.
We have created a class but it doesn't do anything. To make this class more useful we'll need to create an initialize method.
The initialize method is a Ruby class method and is used to initialize variables at the class level. This is helpful when you create an object of the class.
class Mammal def initialize(species) @species = species @endothermic = true @live_birth = true end end
Before we continue, we'll need to look at what we have done above. The first thing that you may have noticed is the @symbol preceding the variable name.
In Ruby, this is called an instance variable and it has a scope limited to the current object. So two different instances of the Mammal class can have different values for their instance variables.
Instance variables are not public. Let's see what happens when we create a cat object from the Mammal class without making it available outside of the object.
cat = Mammal.new("Felis Catus") cat.species # output NoMethodError: undefined method `species' for #
To make the instance variable available outside the object, we will need to create a getter method and we would also need to create a setter method to be able to change the values of the other two instances variables (@endothermic and @live_birth).
Ruby has a clean and elegant way of doing this. Instead of creating methods for setting and getting the value of the instance variables, Ruby has three attribute accessors.
- attr_writer allows you to set the value
- attr_reader allows you to get the value
- attr_accessor allows you to get and set the value
Let's add this to our Mammal class and see what happens when we try to access the value of the instance variables outside the newly created cat object.
class Mammal attr_accessor :species, :endothermic, :live_birth def initialize(species) @species = species @endothermic = true @live_birth = true end end cat = Mammal.new("Felis Catus") puts cat.species # output Felius Catus puts cat.endothermic # output true puts cat.live_birth # output true
We used the attr_accessor method, which will allow us to get the value of the instance variable to set it to something different. If we would want to only allow the value to be read, we would have used the attr_reader accessor.
However, in this example, we want to allow both the reading and writing of the variable because this will come in handy when we reach the polymorphism section.
This should give you a better understanding of Classes and Objects. The next blog will cover Inheritance in our OOP blog series.