What is Object Oriented Programming – Expanded

What is Object Oriented Programming – Expanded

This is the expansion on my previous post which you can find here. This is a broadened look at OOP as it applies to programming in general, not just Objective-C. This is an article I submitted to Helium so feel free to check out my other articles on Helium.

Object-oriented programming (generally called OOP) focuses on the use of Classes and instances of classes called Objects. The biggest advantage to OOP is code re usability. The idea is that Classes can be reused to create multiple unique instances or Objects. Each Object has access to its own unique set of members. The members can be altered within each individual Object. Every class that is created in OOP has the methods to be able to declare, modify, and extend their members or properties.

Another powerful feature of OOP is the use of “inheritance.” Inheritance is a simple concept but proves to be very helpful in developing reusable code. When you are writing your own classes you can provide a Parent class (also called Super Class, generally built into the API) this parent class will usually already have a set of methods for setting up the class. The child (also called Subclass, your custom class) will be able to use all of the parents methods on itself meaning that you don’t need to teach every class how to initialize itself because you can use a parent class that has already defined the methods. By having all classes originate from a parent you can guarantee certain ideas about the classes. If you follow the hierarchy tree for various classes you can see where they originate from and what they may conform too.

Polymorphism is another powerful feature of OOP. Polymorphism works very closely with inheritance in the idea that a general super class can provide a method that does not depend on a particular type of object. The programmer does not have to know what object will be passed to the method in advance which is called dynamic binding. Through inheritance subclasses gain the ability to use their super classes methods, with polymorphism a “general” class can be made that will be able to respond to multiple subclasses that produce different objects. Consider this example: You have a general class called “Automobiles” now you also have 3 subclasses, “Cars”, “Trucks”, and “Motorcycles”. In theory the Automobiles class would act provide a set of general methods that could apply to any subclass. Each subclass would act as a factory for producing its own type of object. You can define several methods in the Automobiles class that will be inherited by each subclass and also use polymorphism to act on different objects like this: Automobiles has a method for Driving, Cleaning the Vehicle, and Filling the Vehicle with gas. Cars will be able to call the driving method since it is inherited; with polymorphism Drive can affect Cars, Trucks, and Motorcycles differently because they are different objects. The same applies to the other methods for Cleaning and Filling. Polymorphism with inheritance allows a class like Cars to inherit from Automobiles without having to define all of the same features of Automobiles.

Encapsulation is yet another powerful feature of OOP. Encapsulation allows a programmer to hide the structure of a particular class or object. The programmer provides methods for manipulating the class but the internal structure of the object is not known to the rest of the program. Encapsulation helps protect the integrity of Objects and to control what data can be sent to the objects. This allows programs to be more complex and yet still maintain the integrity of its objects while preventing other users from putting the data into an inconsistent state.

Here is a diagram from the example:

OOP Inheritance and Polymorphism diagram

Automobiles Example Diagram

The idea of using objects has allowed very complex programs to reuse programming logic instead of having to follow a set procedure. This makes OOP languages very dynamic and easy to update. There are many unique features of OOP that make it so powerful; today OOP is generally the programming paradigm that is taught to new developers. There are many languages that follow OOP for example: Java, C++, Objective-C, Ruby, Python, Smalltalk and many more.

Feel free to comment on any errors, suggestions, etc… and of course Enjoy!

Related posts:

  1. Object Oriented Programming
  2. Code Snippet: Detecting a Custom Button Touch Inside UITableViewCell
  3. UILocalNotifications Epiphany

Leave a Reply

You must be logged in to post a comment.