Object-Oriented Programming (Part 2)
Explained using Java; however, the topics can be implemented in a wide range of other languages, just have to follow the language’s syntax!
Now that we have covered objects and classes, we can begin to deep dive into the final pillars of Object-Oriented Programming: inheritance, polymorphism, abstractions, and encapsulations.
Inheritance is defined as the mechanism of basing objects and class upon another object or class. The definition is much scarier than what inheritance truly is. Inheritance is the idea that a child class takes attributes defined in the parent class, much like how a child has common features with their parents. For example, let’s create an animal class and have a dog inherit its attributes from the animal class.
As can be seen from this example, I can use the attributes from the animal class inside while defining a dog object! Currently, we only have one child class, so writing code like this does not save much time. However, imagine you were in charge of coding a game with an interactive wildlife. By creating parent classes with common animal characteristics and inheriting them throughout, you could minimize the amount of code you would have to write.
In this example, we created an animal class that held an eat and sleep variable. The animal class holds these variables as they are common characteristics for all animals. By writing the common attributes in a single class (Animal class) and extending it to subclasses, we save ourselves from writing repetitive code.
Polymorphism is defined as the ability to take on many forms. We will break polymorphism down into static polymorphism and dynamic polymorphism.
Static polymorphism is achieved through overloading methods. If you read my part one on Object-Oriented Programming you would have already been exposed to the idea of overloading methods ( writing multiple constructors ). Methods with the same name but different parameters are considered to be overloaded. This is extremely useful as depending on the data input an object might be expected to behave in a different way.
Dynamic polymorphism is achieved through overwriting methods or when instantiating objects.
Overwriting methods is achieved by changing the functionality of a method passed down from a parent class. The main use for this is to get more specific with how subclasses interact. Using the animal example we were able to agree that all animals have to sleep, however, a dog and a bat have different sleep schedules. By overwriting the sleep method we can create a more accurate depiction of a bat compare to a dog.
The second reason for using dynamic polymorphism is abstract object instantiation. With dynamic polymorphism, we could do as follows.
The object created is in fact a dog object. The Animal keyword is there to specify the data type. In this case, it is letting us know that the dog will be an object. By instantiating objects like this we can abstract the creation of objects. The following could be achieved.
Here I was able to create one-hundred animals, fifty bats and fifty dogs, with only six lines of code!
Before I begin explaining what abstraction is, I would like to ask you a question. Take your time to think about it before continuing. What is an animal?
You probably struggled to give a clear answer as to what an animal is and that's ok, it is actually what is expected. We all have an abstract idea of what an animal is. On the other hand, if I were to ask what is a dog? I am sure most people would be able to paint a clearer picture of what a dog is and even offer an example.
Abstraction is defined as a process of hiding the implementation details from the user. In layman’s terms, abstraction is creating a base standard for objects, yet, we cannot invoke this standard.
Using this example we can see that the animal class has a base standard for what a dog, a lion, and a bat should be capable of doing. By setting “sleep” as an attribute of animals we create the standard that all animals should sleep. However, from the previous idea of Dynamic Polymorphism, we agree that all animals sleep differently. Therefore, sleeping is an abstract idea that animals have in common.
By creating an abstract class we achieve the following:
- We declare that all subclasses should have these common characteristics
- The abstract class is an idea of what all subclasses are
Creating an abstract class would look as follows
I left encapsulation for last, as it is something we have been doing consistently without realizing. Encapsulation is quite simply how you bundle methods and attributes. This means that all the classes we have created follow the OOP principle of encapsulation.
With this, we have covered the fundamental principles of Object-Oriented Programming. Until next time.