Polymorphism and Interfaces

51:30 AS3 FLA 9

Prerequisite:

Scope and Events in OOP ( ??? );
WARNING: you did not complete all prerequisites of this course. To Complete prerequisites above, pass course Exam.
We are reaching another large step in our training. In the next hour we will continue and build on the OOP skills we developed so far. We will continue and refine our last examples and then move on to a new topic polymorphism. Polymorphism even though it has a very scary name is actually a very easy concept as long as you ignore its name. Polymorphism enables us to treat many object types as if they where one type of object. The idea behind it is to treat objects based on what they do instead of what they are. In the process we will discover the glue that will make it possible - we will create an interface and then integrate it into our logic and show case how it works.

Act now while we have available seating

Our on line school is private, affordable and interactive with trainer support.
Act now save your seat before someone else takes yours.
Act now

What is polymorphism?

The literal translation of polymorphism is "multi formity". Its name is exactly what it is. Lets take a step back to really grasp and understand what this actually means. Until now when programing without polymorphism we had to talk to objects based on there actual class or one of there inherited class line. Polymorphism enables us to group up classes into functionalities. You probably are asking yourself now why would you care to do that, that is a really really good question one that will be really hard to answer in a quick summary but is exactly what we will do in the next hour. Instead of starting from explaining the topic and then implementing it we will go the other way around of first building the elements and then explaining the concept - as the concept is harder to grasp them the actual practice.

What I would encourage you to do more then anything else is to forget about this term for the next hour and just sit back and watch our title. By the end of it you will know what polymorphism is and even how to work with it.

Polymorphisom in code means a way for us not to know what we are working with and instead of that knowing only that the object that we are working with has an API that would work for our needs. This abstraction makes it really easy to work with groups and reduce code footprint between interacting classes and projects. It will be hard to understand fully what polymorphism without knowing what interfaces are and how to work with them.

Working with interfaces

An interface enables us to define a set of methods that are mandatory for a class. In our training we talk about the concept of a night club that has a very strict bouncer that only allows members that have a tie on to enter the club(people how adhear to an interface). If the bouncer had to list out all the tie companies that are approved he would get confused and more then that would probably never manage to manage the door. On the other hand if the club had a strict rule that said each clothing company must comply with a few rules(must implement an interface) and only the ones that comply to these rules may issue a certification tag on the garment then the bouncers job will be really easy all he would need to do is check at the door if the person is with a certificated garment. This is exactly where interfaces (and polymorphism come into play). In code we can create a rule book that we can then enable classes to be "certified" to have a list of public methods. By doing this we now cut the need to even know what objects(classes) are coming in our door all we need to check is if there compatible to our needs. For example in code imagine a radio, dvd and a tv if we have them all certify to our "PLAYABLE" list we can stipulate in our contract with the creators of these devices that they must have a public function called "play" and a public function called "pause". By doing so we can now list in our virtual store many products that we can control using the exact same code without the need to know if we are clicking on a play to a tv , dvd or a radio. In the next hour we will drive through this concept using real cases making it much easier to see this relationship. Once we got an application that is capable of treating an object as something that is "playable" without needing to know what object it actually it is time to cross a line through polymorphism and interfaces as we now know how to create polymorphism in code.


Our free content is sponsored by these ads become a prime member and get rid of them.
Download
Source

Act now while we have available seating

Our on line school is private, affordable and interactive with trainer support.
Act now save your seat before someone else takes yours.
Act now