top of page

&& I OOP...

Writer: Leah
Leah
Mar 25, 2020
3 min read


Object-Oriented Programming, or OOP! Learning to organize our software in terms of objects and classes is something that everyone should understand before you dive head first into it. As more and more arguments emerge on the internet about what paradigm is the best, let's start by understand a few different types and how you can make OOP less of an oops..


When it comes to programming, there are a few different paradigm’s (styles) you can choose from, were going to cover just a couple:

(1) Imperative (2) Declarative (3) Functional (4) Object-Oriented

Now we can start to consider the four principles of object-oriented programming: encapsulation, abstraction, inheritance & polymorphism. Taking each principle and breaking them down allows you the ability to explore the full meanings, as each one seems more complex than the last at first glance. When you take the time to pull these concepts apart you can then fully unlock the power of OOP programming.

Abstraction is where we begin. This is identifying your real-world objects and discern the important attributes and behaviors. So during your analysis & design you pull out critical elements of the classes.

Ex. Class of ‘Ball’ would have the attributes → color & radius; the behaviors → bounce & roll.

To encapsulate is where we put the data & the related behaviors we identified into single data types. So, creating a ‘Ball’ class as a user-defined data type to contain those attributes & behaviors that define the particular type of object. Something like a blueprint to create other objects of that same type. When you use that blueprint to create an object you are creating an instance of that class. The object you create is what is called an instantiation of the class.

Ex. taking our ball example, knowing that not every single ball is the same color & size we can use this to create different instantiations of ‘Ball’ to create different balls:

*Encapsulation is a part of a bigger conversation including Data Abstraction & Data-Hiding that we will get into in a later post.

Inheritance is a powerful mechanism for the modulation of your program. Through the use of inheritance you not only promote code reusability & robustness, but it is also a time saver & can make you a more productive programmer. Inheritance uses ‘is a’ relationships to communicate between the classes.

Ex. Continuing with our ‘Ball’, you may have many different balls as we discussed with Encapsulation, but if you wanted to keep track of all the balls:

  • A ‘tennis ball’ is a ball, but has extra attributes that would differentiate it (material, pattern)

  • So it is almost identical to a ‘Ball’ apart from the extra attributes & operations, allowing us the opportunity to reuse as much of the code from the Ball class by creating an inheritance relationship between ‘Ball’ & ‘TennisBall’

Polymorphism is a key function of object-oriented programming when you are dealing with a program that processes a set of related objects. It is the form of reusing that grants us the ability to design functions and extend operator behaviors to mean different things depending on the context in which they are used. Most programming languages make use of this by either overloading or overriding methods.

Overloading is in the same class, where more than one method that has the same name but different signatures.

Overriding is when one of the methods in the super-class is redefined in the subclass. The signature of the method remains the same.


So essentially, when you run into Polymorphism you need to understand that it is the basic ability to take a base class and redefine the methods for the derived class.

  • The base class method is a virtual class

  • The derived class will override the virtual base class

  • The correct method will be called during run-time based on object calling method

Ex. Still using our ‘Ball’ class :: you could have started with a base class of ‘Toy’ that it could use to calculate the price of different types of balls.

Ex. the English language, & how a verb can have different meanings.

Run has multiple meanings

  • Politicians run for office

  • Plant Managers run a plant

  • Athletes run in the Olympics

In a Person inheritance hierarchy, the derived classes (Politician, PlantManager, Athlete) would need an implementation for the run() method distinct from a general run() method included in a Person base class. IF the method signatures are identical the run() method is an ex. of polymorphic methods.

In the end, using Object-Oriented Programming may be one of the most misunderstood concepts, but it is supposed to match the way people actually think in the real world. As you work with it more and get the hang of how the paradigm works you can break your applications down into bite-sized, solvable problems and build it into a readable structure.


This video from Mosh has to be the quickest description:


Also this article breaks all of this down deeper:


Comments


bottom of page