Object Oriented Programming Concept
Object Oriented Programming, or OOP for short, is using classes to interact with each other and manipulate data. OOP is useful as that it saves time from repeating code. This is especially useful for large scale projects. The example below illustrates a simplified form of the OOP concept.
Let's say you are at work and needed to make a form. So you type out this form on the computer. This form is to be used as a template for making copies of other forms. In programming terms, we can say that this template is like a class . On this form it has areas to fill in your first name, last name, and date. The first name, last name, and date can be seen as instance variables . So now you need a copy of this form. You take a quick jog to the copy machine and make a copy. By creating a copy, you can say you created an instance of an object . So now with this particular copy, or object you can fill out the first name, last name, and date, or in programming terms, fill out the instance variables . If other copies of the template needed to be made for other people, we would make create more copies. Not having a template would mean having to create one every time we needed that form which would be time consuming.
As we can see, OOP enables code to have a huge reusability factor. In the example, the template can be saved and used on a different day by someone else. The template is reused, just as classes are in OOP. Furthermore, OOP let us improve our development speed. The template from the example is created once and copies from the template are created as needed. It will slow production drastically if a form had to be created each time it was needed. Likewise, programming takes time to type and debug. Reusing classes that we already tested as working correctly saves time and produces quicker results. Lower cost in another advantage of OOP. In the example, the form had to be created. If lots of these forms were needed, and we didn't copy these forms, many man-hours would be spent creating the same form, over and over. The company would have to spend money to pay that worker and the resources used to make those forms. It is cheaper to make one and print copies as less hours and resources would be used. In OOP, having a class already programmed works in the same fashion. A class can be created and saved so that it can be used by others in the future.
If the form had to be created for a large project that needed a million of these forms, it will definitely take a huge amount of man-hours to create a form each time. It would be wiser to make one and create copies. It saves time, money, and it can be used again if needed by other people in the future. In the same manner it is advantages for large scale projects to use OOP. It is cost effective, develops software faster, and can be classes can be reused by others in other projects in the future.
Below is a YouTube video explaining Object Oriented Programming in further detail:
VIDEO
Using Classes
To use a class, we have to import the module that the class is located in. If importing from the Python Standard Library we use the keyword import and the module name. The same syntax is used when we import a module located in the same folder as our program. Modules from other libraries can also be used. Using the from and import keywords help python locate these modules. Below are a few examples:
import turtle #module located in Python Standard Library
#has access to turtle module and its classes
import media #module located in the same folder as program
#has access to media module and its classes
from twilio.rest import TwilioRestClient #inside twilio module, then inside rest module
#has access to TwilioRestClient class
from twilio import rest #inside twilio module
#has access to rest module and its classes
After we have access to the class, instances of the class, or objects, can now be created. The variables within the parentheses are the parameters, or arguments, of the class. These parameters are sometimes used in constructor to create the instance variables. The constructor is a special method used to create the instance. Below are examples of creating instances of objects:
brad = turtle.Turtle() #set object brad as Turtle instance
#Turtle class located in turtle module
toy_story = media.Movie(var1, var1, var3, var4) #set object toy_story as Movie instance
#Movie class located in media module
#has 4 instance variables
client = TwilioRestClient(var1, var2) #set object client as TwilioRestClient instance
#has 2 instance variables
client = rest.TwilioRestClient(var1, var2) #set object client as TwilioRestClient
#TwilioRestClient class located in rest module
#has 2 instance variables
With the object instantiated, the methods in the class can also be accessed:
brad.speed(2) #object brad accesses speed method
toy_story.show_trailer() #object toy_story accesses show_trailer method
Creating Classes
A class is typically created by using the keyword class, followed by the name of the class, then the open and close parentheses, ending with a colon. Inside every class is a constructor, which is created as a method using the __init__ keyword and having the instance variables. In the example below, self is used as an instance variable to pertain to the instance in question. So if the object is toy_story and we access the self.storyline of toy_story, we would type it as toy_story.storyline and would access the storyline instance variable of toy_story. A class may also contain methods.
class Movie(): #the class Movie
def __init__(self, movie_title, movie_storyline, poster_image, trailer_youtube):
self.title = movie_title
self.storyline = movie_storyline
self.poster_image_url = poster_image
self.trailer_youtube_url = trailer_youtube
def show_trailer(self): #show_trailer is a method of the class Movie
webbrowser.open(self.trailer_youtube_url)
Inheritance
Say you already have a class. Now you want to create another class that has some of the methods and variables of the first class. We want the second class to inherit the methods and variables of the first class. This is inheritance. The first class is like a parent of the second class. The second class is like its child. Keeping that thought in mind, the example below illustrates how a child class uses the parent class, known as inheritance. It is interesting that class Child has access to the show_info() method even though that method is not in the class Child. Therefore, using inheritance is a way to prevent the repetition of code.
class Parent():
def __init__(self, last_name, eye_color):
self.last_name = last_name
self.eye_color = eye_color
def show_info(self):
print ("Last Name - " + self.last_name)
print ("Eye Color - " + self.eye_color)
#class Child uses class Parent
class Child(Parent): #class Parent in parentheses
def __init__(self, last_name, eye_color, number_of_toys): #class Child constructor
Parent.__init__(self, last_name, eye_color) #uses class Parent constructor
self.number_of_toys = number_of_toys
miley_cyrus = Child(Cyrus, Blue, 5) #object miley_cyrus using class Child
miley_cyrus.show_info() #object miley_cyrus has access to
#Parent method show_info()
#prints "Cyrus" and "Blue"
Method Overriding
Method overriding occurs when the method of a Child class supersedes a method of the same name in the Parent class. In the example below, both class Parent and class Child have the show_info() method. The object miley_cyrus is an instance of class Child, so it will use the show_info method of class Child. If miley_cyrus was an instance of class Parent, it would instead use the show_info() method of class Parent.
class Parent():
def __init__(self, last_name, eye_color):
self.last_name = last_name
self.eye_color = eye_color
def show_info(self): #here is the class Parent show_info() method
print ("Last Name - " + self.last_name)
print ("Eye Color - " + self.eye_color)
class Child(Parent):
def __init__(self, last_name, eye_color, number_of_toys):
Parent.__init__(self, last_name, eye_color)
self.number_of_toys = number_of_toys
def show_info(self): #class Child now has show_info() method
print ("Last Name - " + self.last_name) #which has the same name as the show_info()
print ("Eye Color - " + self.eye_color) #method of class Parent
print ("Number of Toys - " + str(self.number_of_toys))
miley_cyrus = Child("Cyrus", "Blue", 5)
miley_cyrus.show_info() #calls show_info method of class Child and
#not show_info method of class Parent
Object - an instance of a class, sometimes used synonymously with instance
Class - a "blueprint" or "template" used to instantiate objects
Instance - a "copy" of a class, sometimes used synonymously with object
Constructor - the __init__ method of a class used to initialize the instance, receives instance variables
Method - a block of code that performs a specific task in a class, a function in a class
Module - is a file containing classes and/or functions
Library - file containing pre-programmed modules used to help program
Class Variables - variables defined within a class but outside any method, shared by all instances of the class
Instance Variables - parameters, or arguments, necessary for creating an instance, used in the constructor
Inheritance - where one class has access to attributes, such as variables and methods, of another class
Method Overriding - where the method in the Child class supersedes a method with the same name in the Parent class
Abstract thinking tends to have an overview of events and occurrences. Instead of using concrete quantities, abstract thinkers tend to place emphasis on the meaning behind the quantities and the relationships of. If Juan is 35 and Marcos is 4, an abstract thinker would think of it as the age of Juan is thirty-one years older than Marcos. This shows a relationship between Juan and Marcos.
Using abstract thinking in programming, we can have Marcos_age = 4, and Juan_age = Marcos_age + 31. This shows the relationship between the two variables. We can show a deeper relationship as we enter Object Oriented Programming. Using the Juan and Marcos example, we can show a Parent and Child relationship. Marcos can inherit the hair color, last name, and eye color of Juan. This abstract thinking leads to showing and exploring relationships which is the main concept of object oriented programming.
This concept can also be seen in HTML and CSS. HTMl and CSS has the relationship where HTML is the content of the webpage and CSS is the styling of it. HTML accesses the styling selectors in the CSS to determine the styling of its content. The styling selectors are like tasks which determine the styling of the HTML's content. This is similar to programming where classes are accessed so that certain tasks can be performed. We can see that the relationship between an HTML and CSS is similar the classes being accessed in object oriented programming.