Inheritance vs Polymorphism

Basic of Object Oriented Programming

Object Oriented Programming or OOP is a programming concept that is being used widely in many different programming languages and that includes Swift. Through OOP, programmers are able to create objects with data containing attributes allowing the program to communicate back and forth.

But why OOP? The reason behind is to break apart a complex program into smaller blocks making it a lot easier to develop and manage. 

Some of the most common use of Object Oriented Programming are:

Inheritance

A class called subclass can inherit method, properties and other attributes from another class which is known as the superclass. For more information about Inheritance, check out the tutorials that I’ve wrote here. Swift could only support single inheritance and not multiple inheritance but if you would like to use multiple inheritance, there is a way to go around. The example below here is using only single inheritance.

The output of the program would look this:

Polymorphism 

The ability of a subclass inheriting specifically the same method and returning a different output. For more information about Polymorphism, feel free to check out my other tutorial here. Here I created an Animal class with a method of speak. I’ve created several animals, and wanted them to speak differently.

The program will then output the following:

Differences between inheritance and polymorphism

To some it could be a little confusing as Inheritance & Polymorphism could look a little similar to each other. One thing to always remember is that Inheritance often means the usage of the structure and behavior of a superclass in a subclass, while, Polymorphism means the behavior changes of a superclass in a subclass.

 

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

One thought on “Inheritance vs Polymorphism

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*