The
majority of the semester, except for this week and last week when we first
dipped our toes with recursion, consisted of learning exactly what object-oriented
programming is. If I were a betting man I would say that the expectation of
this course is that we are able to fully understand and be able to explain what
object-oriented programming, or “OOP” for short, is. Here is my best shot at
it.
The
name of it is pretty self-explanatory. OOP is a type of programming process that
revolves around devising procedures using objects as the information to solve
real-life problems. Whenever the instructor threw around big words such as “Abstract
Data Types” or “methods”, I reminded myself of the actual “Object-Oriented”
part of the OOP name. What are objects? They are basically chunks of data you
can store, manipulate, and extract. They can be Python built-in data types,
such as integers, strings, and Boolean values, or you could even make your own.
These are called Abstract Data Types, or “ADTs” for short. These store the
intended meaning of the data it stores and what to do with them. Each ADT is
organized as a “class”, which contains and neatly organizes “methods”. These are
functions that can either report some property of the object the class
represents, or mutate it. This all seemed super complicated to me at first
because just the terms and concepts seemed super abstract. I fixed this by diving
into analyzing examples, which the instructor was more than generous about. One
of the examples was a Point object. A “Point” has coordinates, which has to be
initialized first (I will explain later how that works). We also want its
distance from the origin. A class would provide these services, both
initializing the coordinates and returning its distance from the origin,
through the methods that it houses. Not so hard to understand anymore, eh? Of
course, we can get into much more detail with __eq__ and ___repr__ special
methods and how to call upon the methods to use their services but the
fundamental idea is that the classes and methods all revolve around how to
perform operations with and/or on the main object that they were created to do,
hence OOP.
Inheritance,
though, is a concept worth mentioning and the explanation of OOP would not be complete
without it. Sometimes, we have an object that is kind of like one thing but is
more specific. For example, a square and a triangle are shapes but they are
more specific in that they are specific shapes. They share general attributes
but determining them might require operations specific to their needs. The
solution here is to create a parent class Shape and still implement methods.
Then we can call Square and Triangle “subclasses”, which means that they
inherit the methods but when we program the subclasses, we can override the
inherited methods and add new methods that are only for that subclass. The
endgame is that programmers are able to fully customize each class while doing
minimal work if possible. Sounds like a perfect deal to me.
Hopefully
my explanation of OOP was clear enough for my classmates to understand. I know
this stuff will come up on the final exam so I definitely look forward to
coming back to this entry to review OOP concepts. I feel I can do quite a few
things with this knowledge. As a matter of fact, my group (with Lukas and
Jovan) did quite well in the first assignment, which dealt with practicing OOP
so I think I’m set. I now look forward to more recursion to add to my toolbox along with OOP.
No comments:
Post a Comment