Principles of Object Oriented Programming

Aug 11th 2016 by Beej Burns

A Brief History of Object Oriented Programming:

SIMULA was the first object language created to build simulations. Alan Kay and Xerox Parc enthralled bu the object language developed the Dynabook with Smalltalk as an object-oriented language for programming it in the 1970's. In the 1980's Bjorn Stroustrup integrated OOP into the C language resulting in C++.

Object Oriented Programming has four major principles: Abstraction - using a focused representation of an actual item, Encapsulation - hiding the internals of an object, Inheritance - reusing code from an existing object, and Polymorphism - one name, many forms.

These tend to come up a lot in interviews especially for junior positions as they are an easily quizable subject.

Episode Breakdown

18:29 Abstraction

“An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of object and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.” — G. Booch

Abstraction is the focused representation of an actual item. It takes a complex reality and puts it into the terms of a simplified model. The goal is to reduce the complexity while still keeping the base characteristics.

When abstracting you are developing in terms of interfaces and functionality instead of implementation details. This separates the what you are creating from how you create it and allows focus on one aspect at a time. Doing so allows the properties needed to be highlighted while hiding the rest. Abstraction hides the details of an object only exposing the essential features.

25:16 Encapsulation

Similar to abstraction encapsulation hides the internals of an object from outside view to protect the objects integrity and reduce system complexity. When encapsulated the changing internals of a class does not effect the outside code. Also changing the implementation does not reflect on clients using the methods.

Access to encapsulated objects is restricted to accessors and mutators. An accessor is a method that asks the object about itself. These are also called 'getters' as they typically use the get method. However any public method giving information about a private object can be an accessor. Mutators are public methods that alter the state of a private object while hiding the implementation of the object. These are also called 'setters' as they use the set method.

30:42 Inheritance

Inheritance is the reuse of existing objects to create subclasses that inherit the attributes or behaviors of the existing classes. It allows the reuse of software to capitalize on the efforts that went into the original software and helps to eliminate redundant code.

Superclasses are the classes which are inherited from and establish a common interface. They can be be abstract classes that are declared but have no implementation of their own. Subclasses are derivatives that inherit from another class. They inherit the complete interface and implementation. However they can override and change the functions from the superclass. Any function not overridden is inherited from the superclass.

Some languages allow for multiple inheritances while others do not giving rise to inheritance hierarchies. If C inherits from B, and B inherits from A, then C inherits A as well.

44:23 Polymorphism

"Polymorphism, a Greek term, means the ability to take more than one form." ~ Kaaria Muriungi

One name, many forms. Multiple methods may all have the same name but different behavior in different instances based on the state of the object. This also allows the ability to process objects differently based on their type or class.

The two types of polymorphism are overriding and overloading. Overriding is also known as run-time polymorphism because the method used is determined at run-time based on the dynamic type of object. Overloading is compile-time polymorphism where the compiler determines which method will be run at compile time.

IoTease: Project

Custom Xbox Controller for Persons with Disabilities

Caleb Kraft modifies game controllers for people with disabilities to allow them access to all the features of the controller. For this project he added buttons by the joy sticks for the shoulder buttons and joystick press then rubber feet to the triggers so they could be pressed by setting the controller on a surface and rolling it in the direction of the trigger. He does this volunteer work in his spare time. Such a cool use of his creativity.

Hardware

  • 4 Buttons
  • Wires
  • Rubber Triggers

Tricks of the Trade

Complete Developer Podcast