What is Object-Oriented Programming?
Object-oriented programming (OOP) is a paradigm or design that allows us to model concepts as objects and define the relationships between the objects. The paradigm focuses on the objects developers want to manipulate, instead of the logic required to manipulate them. An object in OOP is defined as a data field with unique attributes and behaviors.
Why do Coders use OOP?
Developers use this approach when programming large or complex projects that need maintaining. Languages like Java, C++, Python, Ruby, Scala, and PHP are all OOP languages.
How is Python an OOP?
In most cases we will start with something called a class, which are usually things that we can represent as nouns. For example, we can have an animal class, a book class, or a player class. In this article, we use the American hip hop trio, Naughty By Nature, as tribute to their first hit, “O.P.P.”, along with other examples to help better understand the concepts.
Classes are made up of two main parts. First we have our attributes. Attributes are also nouns that our class has. If our class were a soccer player, his or her attributes would be health or speed. The attributes for our Naughty By Nature class would be the names of the individuals who make up the group, Treach, Vin Rock, and DJ Kay Gee.
The second main part of a class are its methods. These are verbs, actions, or behaviors that we define for our class. For a soccer player, this can be run or kick. The methods for our Naughty By Nature class will be sing and dance.
To sum up, in OOP we will be writing one or many classes which are made up of attributes and methods.
class NaughtyByNature: # init works as a constructor to initialize the class object # we list all the attributes we want our class to have # we take in the initial values as parameters and then # assign the parameters to the actual attribute themselves def __init__(self, treach, vin_rock, dj_kay_gee): self.treach = treach self.vin_rock = vin_rock self.dj_kay_gee = dj_kay_gee
The Four Principles of OOP
Encapsulation
Encapsulation prevents data from direct access. Let’s say we create a class from another class, with encapsulation we cannot alter the parent class data from the child class. Encapsulation occurs when hiding the implementation details of an object.
Because Python does not have the ‘private’ keyword like other OOP languages, it does encapsulation by using a class variable that is prefixed by an underscore.
class Encap(object): def __init__(self): self.__update = 9 def getUpdate(self): print(self.__update) def setUpdate(self, update): self.__update = update obj = Encap() obj.getUpdate() obj.setUpdate(10) obj.getUpdate() print(obj.__update) # Prints #9 #10 #Traceback (most recent call last): # File "main.py", line 15, in <module> # print(obj.__update) # AttributeError: 'Encap' object has no attribute '__update'
However, we would be able to see three values and no error if all underscores were removed.
Abstraction
We can think of abstraction as only knowing certain things to get a job done. You may know how to drive a car, when to break, accelerate, or how to put on your seat belt, but you may not know all of the inner working of the car since you do not really need to in order to drive it.
To use abstraction, we need to import the abc module. Below is a code example of an abstract base class.
import abc class AutoMobile(metaclass=abc.ABCMeta): @abc.abstractmethod def speed(self): pass class Car(AutoMobile): def __init__(self, x, y): self.a = x self.b = y def speed(self): return self.a * self.b c = Car(1,45) print ('speed: ',c.speed()) # returns speed: 45
Inheritance
Inheritances occur when properties of a parent class are passed along to a child class. There are five different types of inheritance in python, they are single, multiple, multilevel, hierarchical, and hybrid inheritance.
Below is a simple example of single inheritance.
class Parent(): def mom(self): print('Parent DNA') class Child(Parent): def kid(self): print('Child that only has 1 parent') obj = Child() obj.mom() obj.kid()
Polymorphism
Polymorphism is the ability to take on many different forms. In Python code, that means the ability to define methods in the child class with the same name defined in the parent class.
Below is an example of polymorphism. Notice the type() function in the Pizza and Burger classes.
class Pizza(): def type(self): print("Pineapple") def carb(self): print("dough") class Burger(): def type(self): print("Black bean") def carb(self): print("rye") def func(obj): obj.type() obj.carb() obj_pizza = Pizza() obj_burger = Burger() func(obj_pizza) func(obj_burger)
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.