Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs.
The basic programming concepts in OOP are:
The first principle of OOP we are going to cover is data abstraction. Data abstraction is just a fancy way of saying that we are going to model our data to look like it’s real life equivalent. So, how do we model a drink? Well a drink should be an object that has a name, size, ingredients, alcoholic constant. It should also be able to give details about itself. Translating this into Ruby and we get something like this.
This is what is called a class. A class is just a blueprint that is use to create objects that represent some structured data which in this case is a drink.
Wrapping up the data into a single unit is known as Encapsulation. It can also be called as the procedure to bind code and the data which is affected by the code together. By applying encapsulation you can protect your data from being manipulated by another source. In simpler words, it can be considered as the mechanism which allows data only to be manipulated by the member functions of the class in which they are declared.
You can achieve encapsulation by declaring all the variables of class as private( they are by default private) and all the member functions as public(they are by default public). Now, these variables will only be accessed by these public methods of the class. Let’s say that we have two new drinks. martini and macchiato as mach .
Here the instances of class “Drink” are martini and mach.
in the above code you can observe that the member functions and data are only accessible through the object or instance of the class.
for example; in pry,
martini.name will give us “Dirty Martini” not for the mach’s name “Macchiato”
Inheritance is a relation between two classes. We know that all cats are mammals, and all mammals are animals. The benefit of inheritance is that classes lower down the hierarchy get the features of those higher up, but can also add specific features of their own. If all mammals breathe, then all cats breathe.
Though we didn’t specify how a Cat should breathe, every cat will inherit that behavior from the Mammal class since Cat was defined as a subclass of Mammal. (In OO terminology, the smaller class is a subclass and the larger class is a super-class. The subclass is sometimes also known as a derived or child class and the super-class as base or parent class). Hence from a programmer’s standpoint, cats get the ability to breathe for free; after we add a speak method, our cats can both breathe and speak.
Polymorphism is the ability for different types of data to respond to a common interface.
Here’s a simple example in Ruby with inheritance :
As you can see, we sent the same message to different object and got different result. The
OOP in Ruby - object-oriented programing in Ruby
last modified September 9, 2020 In this part of the Ruby tutorial, we talk about object-oriented programming in Ruby…
Object Oriented Programming in Ruby by Example
There is lots of information out there about object oriented programming (OOP), but if your like me I find most of this…
Encapsulation in Ruby
Ruby | Encapsulation: In this tutorial, we are going to learn about the encapsulation, how to achieve encapsulation…
Ruby Inheritance, Encapsulation and Polymorphism - Devblast
Second article in the Prepare for a Ruby job interview series. Today, we're gonna talk about Inheritance, Encapsulation…