Lesson 4: Object Oriented Programming
Object-orientation is a programming paradigm that allows programmers to translate real world concepts and relationships into logical structures and computation. The paradigm not surprisingly focuses on the concept of an “object. “Objects” in a program are basically abstractions of objects in the real world. In sales applications, customers are depicted in the program as customer objects. In a maps application, buildings are represented by building or structure objects. Furthermore, in the software you are using to read this text-book, each character, word, paragraph, button and page has a corresponding object representation in the code. These object representations then go on to interact with other object representations in the program by editing both its own and other object’s contained data. Objects can contain other objects or even replicate and extend the functionality of another object. Whatever real world problem you're given, object-orientated programing allows you the programer to take the tangible items involved and simulate their properties and relationships in your code.
Let start with an high level example depicting how to approach a given project in an object oriented manner. Imagine you're given the task to create software that manages an animal rescue shelter. The best place to start conceptualizing your program is to look at the “objects” and “relationships” that exist within the shelter. Write out the problem in words and look for the nouns. The shelter itself, that could be the all encompassing object that all other objects in the program operate within. We can then break down that noun into its parts. The shelter obviously consists of animals so a shelter object can consist of an array of animal objects. The shelter also provides cages that each animal sleeps in so we have another array of cages that we can map each corresponding animal to. The cage object in the program can have a reference to the animal object and in turn the animal object can have a reference to its cage. One cage can have only one animal and one animal can only have one cage. This is called a one to one relationship (we will explore more types of relationships later in this chapter).
Once we have drawn up all the objects such as employees, volunteers and bags of kibble, we can look for adjectives and attributes that can be useful in describing these objects in the program. An animal has an age, a name, a count of how many times it’s been outside during the day, the time of its last meal etc. These attributes then become the properties of the object’s representation in the code. These properties can be unchanging aka constants in the object, for example an animal's fur color, or can be malleable like when a volunteer object performs a feeding action on an animal object, the last meal time value associated with the animal is updated.
This example can continue to be fleshed out going into how animal objects with a name and age can be extended into dog and cat objects that have extra fur color and leg properties. Bird objects also can extend an animal object but instead adds wings and feather color properties. Dogs objects can then perform actions depicted as methods in the code such as eat kibble which updates its internal number of times eaten value but also decrements the count of number of available kilograms of kibble there is in a Kibble Bag Object. Object oriented programing is a vast discipline consisting of decades of research. The best way to approach it and figure out its rules is to look at the problem’s real world objects first, describe them and their relationships in your own words, then research object oriented principles that can help you simulate them in your code.
Objects as seen by a Computer
Now we have an idea what real world things are simulated in a program, let’s discuss how they are simulated and represented in code.
Why use OOP?
Two simple motives:
Code duplication is a bad.
Code will always be changed.
OOP provides code reusability which reduces the code duplication because once you have it duplicated, you have to make changes everywhere which degrades the application maintainability. Application's code and requirements can be changed anytime so when you want to make changes in your application, OOP makes it easier.
Features
There are three main features of OOP:
Class: It is like a blueprint of the object that you want to create. It should have variables and functions related to the object.
class Dog { ... }
Instance: It is when you initialize the class in the runtime of the application.
let Lassie = Dog()
Methods: It is one way that you can change or do actions with the created object.
Web/Javascript ES6
class Dog { constructor(name) { this.name = name; } bark() { console.log("woof"); } }
iOS
class Dog { name: String = "" func setDogName(name: String) { self.name = name } }
Android
class Dog { String name = ""; function void setDogName(String name) { this.name = name; } }
Access Control: Control the accessibility of variables or methods in your class. Only allow the minimum necessary access, avoid make everything public.
Web/Javascript ES6
There is no native support for private properties with ES6 classes
iOS
public class Dog { private name: String = "" public func setDogName(name: String) { self.name = name } }
Android
public class Dog { private String name = ""; public function void setDogName(String name) { this.name = name; } }
Abstraction: The concept of abstraction hides all but the relevant data about an object in order to reduce complexity and increase efficiency. (Consult branch oop/swift)
Polymorphism: It's a concept that allows one interface to be used for a general class of actions. It’s an operation that may exhibit different behavior in different instances. (Consult branch oop/swift)
Inheritance: The process by which one class acquires the properties and functionalities of another class. Inheritance provides the idea of code reusability and each sub class defines only those features that are unique to it. (Consult brach oop/swift)

Advantages of OOP
OOP provides a clear modular structure for applications which makes it good for defining abstract datatypes where implementation details are hidden and the unit has a clearly defined interface.
OOP makes it easier to maintain and modify existing code as new objects can be created with small differences to existing ones.
OOP provides a good framework for code libraries where supplied software components can be easily adapted and modified by the programmer. This is particularly useful for developing graphical user interfaces. Consult Javascript Design Patterns for web, React Design Patterns for React, Swift Design Patterns for iOS and Java Design Patterns for Java.
On our class example we will use
Javascript
Classes
Instance
Scope (this, local, global)
Swift
Classes
Instance
Scope (self, local, global)
Callback
Android
Classes
Instance
Scope (this, local, global)
Callback
Last updated