Introduction to Object Oriented Programming Concepts
What is a "programming paradigm"?
A programming paradigm is a fundamental style of coding. The paradigm(s) chosen in a language can drastically change how programs are implemented in that language, and how problems are solved.
Depending on who you ask, you'll probably get a different answer to "how many programming paradigm categories are there?" I'd like to say there are four main categories:
Procedural (Imperative) Programming Languages
Procedural programming languages (also known as imperative languages) are programming languages in which the primary program unit for solving problems is the procedure. In other words, we look at a problem and then break up the problem into chunks that are solved by procedures. A procedure is a sub-program. In a language like C or C++, for example, the global function would be an example of a procedure in code.
Although some people make distinctions, as mentioned above, imperative languages also typically refer to procedural languages and vice versa. The reason the term imperative is used is, like in grammar, commands are made - you tell the program to "do this, then do that, then do this..." You give it step-by-step instructions on how to accomplish a task
Functional Programming Languages
A functional programming language is a language that typically avoids maintaining state information, which is in stark contrast to imperative (procedural) languages which rely heavily on state change information. Programming in a functional language relies heavily on the application of functions to solve tasks.
A very popular family of languages that rely heavily on the functional paradigm are the LISP languages (LISP, Common LISP, Scheme, Clojure, etc.) These languages rely heavily on list processing.
Some consider declarative languages, such as SQL, to be a subset of functional languages.
Logic Programming Languages
Logic programming languages tend to be some of the more unusual languages. Instead of giving direct procedures for performing calculations, they rely heavily on making relationships between data entities. The syntax many of these languages use is based off of predicate logic, which allows for precise definition of rules and relationships.
PROLOG is one of the most popular logic programming languages.
Object Oriented Programming Languages
An object oriented programming language, or OOP language for short, is a language that focuses on objects, which are self-contained entities that interact with one another. Although these entities perform actions often in a procedural fashion (such as the use of member functions in C++, or as they are called in languages like Java or C#, methods), they make frequent and comprehensive use of the nature of the objects themselves.
OOP is typically realized, in languages such as C++, C#, and Java, in the form of writing classes, which are like blueprints of what a particular object is supposed to look like, do, calculate, etc. An object is an instance of a class. For example, I might have a Person class that has name, age, and weight as private data members, and has member functions (methods) that get and set that data. Then, if I create an object named myPerson, this is an instance of the Person class. Usually this is done in a declaration form such as:
Object oriented programming has three primarily pillars, or characteristics that are often used:
Encapsulation involves grouping attributes (data) and the methods (member functions) that act upon that data together. This characteristic of OOP gives us two distinct advantages:
- Information hiding, which is the principle in which we protect private data inside the class from inadvertant modification from the "outside world" (code outside the class)
- Bundling of data and methods related to the object
Inheritance is a form of software reuse, in which classes (the blueprints for objects - in other words, the definition of what an object should be like) can inherit behaviors (and sometimes data) from other classes. For example, if we know that all Animals eat, drink, make noise, and sleep, we could create a general Animal class that other specific classes like Dog, Cat, and Horse inherit from. These specialized classes would then expand upon the basic "Animal behaviors", but they'd all have the Animal behaviors (and sometimes data) in common.
Polymorphism is the most complicated of the three OOP pillars. It requires a good understanding of inheritance in order to understand it well. Think of the example from earlier, where we have an Animal class, known as the base class (or superclass) and we have derived classes (also known as subclasses), Dog, Cat, and Horse.
Polymorphism allows us to use a reference type of a base class to point to a reference type of a derived class. For example,
Animal* a = &dog;
Animal* b = &cat;
And, given certain conditions (explored in later articles), we can utilize dynamic binding, which means at runtime, the program picks the correct form of a method to use. For example:
will make two different noises, because a points to a Dog object, and b points to a Cat object. This is quite a powerful feature, which I'll explore in a future article.