Object-Oriented Programming and Dynamic Binding in OOP.

Hello, Folks. In this article, I wanted to dig deeper into concepts of Object-oriented Programming (OOP) and dynamic binding for the purpose of in-depth study and teaching. I hope that you will learn new things or refresh your knowledge through my articles. Meanwhile, this article will talk about dynamic binding too. If you have no information about binding, I would recommend reading my article called variables.

Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).

Many of the languages that support OOP are high-level languages with a multi-paradigm concept. Examples of these languages are Java, Python, and C++. With the advent of OOP, programming has become easier, faster, more secure, and dynamic. This explains why the languages I have listed are on the list of most used languages today. OOP has four major building blocks, which are Abstraction, Encapsulation, Inheritance, and Polymorphism.

Abstraction — Objects reveal mechanisms that are relevant for the use of other objects, hiding unnecessary implementation code. This concept is thought of as an extension of encapsulation and helps developers more easily make changes and additions over time. We can guess what it does when you press any specific button on the phone, but its implementation is hidden from us. For example, when you press the home button on the iPhone, it can give different results in different places, when it is in the program, it returns to home, but we can not see how it is implemented.

Encapsulation — Having logically-different objects within the class, which can neither be accessed nor modified by other classes. The implementation and state of each object are private, as they are defined within the class. The object can manage its state through methods. With this data hiding feature, encapsulation provides high security and avoids unintended data corruption. Three main data modifiers are used during encapsulation. Java supports four modifiers for the visibility of classes, methods, and attributes.

Public — Classes, methods, and variables can be accessed by all the classes.

Private — Most restrictive modifier, which methods and variables can be accessed within the same class.

Protected — Methods and variables can be accessed from the package by classes and subclasses.

No Modifier — Or called package-private because when you don't use any modifiers, you can access it within classes from the package.

Inheritance — When objects are similar or share some common logic, but not entirely the same, the inheritance comes to help. We create a child class, subclass, from the parent class, superclass. Through this way, we form a hierarchy. Let’s say we have the person superclass, and teacher and student inheriting from that superclass.

Polymorphism — Polymorphism means existing in many forms. Let’s say we have a parent class and several child classes. We would like to use the method of the parent class with some modifications for each subclass and each subclass can keep its own version of these methods. We create three objects and treat them like the same type of object. After writing the appropriate methods for each, for example, If the object is triangle, the CalculateSurface() method for the triangle, and if it is circle, the method implemented for the surface will be called. This is called overriding, the ability to define a behavior that’s specific to the subclass type. So subclass can implement a parent class method based on its requirement.

Dynamic binding (dynamic dispatch/run-time binding/late binding) — the process of linking procedure call to a specific sequence of code (method) at run-time. So all calls to overridden methods are resolved at run-time. Let’s say we have a superclass Animal that has a move method, and subclasses of Cat and Fish (with the implementation of move methods in each class).

Animal myAnimal = new Fish(); 
myAnimal.move(); /*calls the method of Fish, not animal. */

Now let’s add Pirana that derives from Fish, which doesn’t override the move method, but has an extra method called bite.

Animal myAnimal = new Pirana(); 
myAnimal.move(); /*Pirana doesn't override move, look for one-level up. */

Now let’s try this:

Animal myAnimal = new Pirana(); 
myAnimal.bite(); /* this won't compile, because myAnimal is a
variable of type Animal and Animal doesn't have the method of bite*/
((Pirana)myAnimal).bite(); /* We need to cast! or we can create an
object for pirana. */
Pirana p = (Pirana)myAnimal;
p.bite();

I hope this article has shed light on some dark parts of OOP in your brain. Stay tuned for new ones. Peace ✌🏼!

Master of Computer Science and Data Analytics at ADA/GW Universities, Researcher, Psychology-lover, Meme-maker, Musician, Writer, AI & Sarcasms!