Pillars of Object-Oriented Programming

Object-oriented programming (or as I will be referring to “OOP”) is a programming paradigm based on the concept of objects. Objects can contain attributes or properties and methods. The most popular OOP Languages are class-based, meaning that objects are instances of classes.

Common OOP Languages

  • Java.
  • JavaScript.
  • Python.
  • C++
  • Visual Basic . NET.
  • Ruby.
  • Scala.
  • PHP.

Objects

The introduction of objects built on structures, storing different primitive types together. OOP offered creating and holding methods for these types. Objects are instances of a class while classes are a template for an object. A feature of objects is that an object’s own methods can access and modify the properties of itself by using things like this or self.

4 Pillars of OOP

Encapsulation

Encapsulation represents the bundling of data and methods in a class. Essentially it is the delegation of data inside a class that is not able to be accessed by anything outside of the class. You can still interact with an instance of a class, but you need to do so with that classes methods.

Getter and Setter

Getters are methods in a class that get information while setter methods change the value of that information. To make information “read only”, you will define a getter method with no setter method.

Separating information into a class will benefit a program by preventing strange or unwanted errors or your program becoming tangled.

Abstraction

Abstraction refers to keeping only essential details available and keeping everything else hidden.

Users of the classes should not have to worry about how the class is functioning. The only concern should be knowledge of how to use the class. Programs are becoming more complex and require multiple developers to work on one project. It would be best if your section did not rely on knowledge of the other developers sections inner workings.

Interface vs Implementation is a way to think about this concept. Interface would represent the way sections could interact with each other. The implementation of the class should be hidden as other parts of the program do not need to know about how the section works under the hood. The key of abstraction is to create points of contact that act as interface between any classes, and worry about implementation only when programming it.

Inheritance

Inheritance is the principle that allows classes to derive from other classes. Classes can inherit methods and properties from a class like a parent-child relationship. A way to think about this is you could have a pet class that has attributes such as has_fur? and has_4_legs? as well as functions such as walk and speak. You could then create separate animal classes that can inherit all of these things as well as more specific properties and methods for the new class. For inheritance the parent class is referred to as the superclass while the child is referred to as the subclass.

Access Modifiers

Access modifiers change which classes have access to other classes, methods, or attributes. There are 3 main access modifiers:

  • Public: A Public class can be access anywhere in the program.
  • Private: A Private class can only be accessed from within the same class where the Private is defined.
  • Protected: A Protected class acts as a middle ground. These can be accessed by any class it is defined, as well as subclasses of the class. Protected classes are private to the hierarchy it resides in

Polymorphism

Polymorphism describes methods that can take on many forms.

  • Dynamic polymorphism: occurs during the runtime of the program. This is created when a superclass and subclass share a methods name and parameters but have different implementation. In polymorphism the subclass method would override the superclass method of the same name. Which version of the method you run would depend on the level of the hierarchy you are in.
  • Static polymorphism: occurs during compile-time. This refers to multiple methods with the same name but different arguments are defined in the same class. This is also known as method overloading. The appropriate method would be called based on parameters are given to it.

Student at The Flatiron School in Washington DC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store