lesson 8: the oop concepts of polymorphism and interfaces

18

Upload: lucas-caldwell

Post on 17-Jan-2018

254 views

Category:

Documents


0 download

DESCRIPTION

Scenario: At a supermarket, there are three people working there. They all serve a single purpose: to do their jobs that they have been hired to complete. However, what their job actually entails is completely different. The jobs are: - manager - shelf stocker - cashier

TRANSCRIPT

Page 1: Lesson 8: The OOP concepts of Polymorphism and Interfaces
Page 2: Lesson 8: The OOP concepts of Polymorphism and Interfaces

Lesson 8: The OOP concepts of Polymorphism and Interfaces

Page 3: Lesson 8: The OOP concepts of Polymorphism and Interfaces

Scenario:

At a supermarket, there are three people working there.

They all serve a single purpose: to do their jobs that they have been hired to complete.

However, what their job actually entails is completely different.

The jobs are:

- manager- shelf stocker- cashier

Page 4: Lesson 8: The OOP concepts of Polymorphism and Interfaces

Scenario:

The supermarket company who employs them for their skills and experience in these fields will command them to "do their job".

Each of the three people will then do a different job at the supermarket.

The employer cannot just tell any random person to "do their job".

The people waiting for those types of commands must satisfy the following condition before anything can happen:

- be a part of the company

Page 5: Lesson 8: The OOP concepts of Polymorphism and Interfaces
Page 6: Lesson 8: The OOP concepts of Polymorphism and Interfaces

Code demonstration

Implement a new job

- DELIVERY DRIVER

And then reiterate the presentation from the beginning again to enforce concepts.

Page 7: Lesson 8: The OOP concepts of Polymorphism and Interfaces

What defines an Interface?

-An interface is used to standardize variable names, function names, parameters and return values through their "signatures" that have to be implemented

-Implementing an interface means copying mandatory signatures of responsibility in implementing classes

-All implementing classes have the same function names but the specific jobs or instructions that those functions do are very different from each other.

i.e. cashier, manager, shelf stocker

Page 8: Lesson 8: The OOP concepts of Polymorphism and Interfaces

Difference between EventListener & Interface

An Event Listener is OPTIONAL. You're listening for instructions and you can do whatever you want with those instructions, if you want.

Once you implement an interface, all its signatures of responsibilities listed inside the interface becomes MANDATORY to copy into the class implementing the interface.

Page 9: Lesson 8: The OOP concepts of Polymorphism and Interfaces

Difference between EventListener & Interface

Using an interface is not applicable in all situations and is usually found in advanced framework environments like game engines.

Interfaces are also commonly used in architectural design patterns.

Most of the time, an event listener will essentially do the same job as an interface with less strict requirements and still fulfill the purpose.

Unlike optional event listeners, interfaces are used when methods are strictly required to be implemented without question to aid in the full functionality of developing an application.

Page 10: Lesson 8: The OOP concepts of Polymorphism and Interfaces

Unless you're developing a framework, you don't have to worry about calling classes that have implemented an interface.

Most developers only have to worry about implementing interfaces and not designing it.

i.e. Android developer framework client side:

- onCreateApplication- onPauseApplication- onApplicationExit- onApplicationResume- onApplicationLowMemory

Page 11: Lesson 8: The OOP concepts of Polymorphism and Interfaces

The reason it's called an interface is because a set of predefined signatures of responsibility are provided for you to "interface" with using your own instructions in response to each of them.

Think of a power socket. It has a predefined size for any power plug receptors that are expected to interface into it.

Page 12: Lesson 8: The OOP concepts of Polymorphism and Interfaces

Implementing an interface basically means how a class will react when specific instructions are sent to them.

An interface is a mandatory way to standardize return values, variables, functions, function names and parameters so that there are no ambiguities whatsoever when you're designing your application.

Page 13: Lesson 8: The OOP concepts of Polymorphism and Interfaces

What are the uses of an interface?

There are two uses for interfaces: Functional or architectural.

Page 14: Lesson 8: The OOP concepts of Polymorphism and Interfaces

Functional Interfaces

Functional Interfaces actually physically do something because commands are sent to them.

Functional interfaces are usually used in frameworks where you have to design a set of standardized function names, parameters and return values so that the framework can run your command.

In real life, the framework doesn't usually know who has implemented their interface so they perform something called "self reflection" to self examine which classes in a package have implemented their interface before sending out any commands to them.

This is the major difference between using event listeners and interfaces.

Page 15: Lesson 8: The OOP concepts of Polymorphism and Interfaces

Architectural Interfaces

These don’t usually physically do anything.

Sometimes, programmers/developers work with project architects/project managers who "own" the project you're working on and is directly responsible to the company for the completion of the project.

They are responsible for designing the core architecture and distribute workloads to subordinate programmers/developers to build different solution chunks for the overall application or "problem".

Page 16: Lesson 8: The OOP concepts of Polymorphism and Interfaces

Architects may assign you an interface to implement as a formal set of instructions for how to name the publicly exposed APIs so you don't end up giving the power on function some ambiguous name like "igniteArsenalRocketSystems" instead of "powerOn".

They may require you to follow a strict interface when you don’t trust you to name your own functions.

They may use an interface to instruct you to create 10 publicly exposed methods that are required to return certain data type values.

It doesn't matter if you have to create 50 private internal methods to make those 10 public methods work but those 10 methods must meet the standards of the interface.

Not fully complying with the interface will cause compiler errors.

Page 17: Lesson 8: The OOP concepts of Polymorphism and Interfaces

Naming conventions for Interface Classes

Interface classes by convention use an uppercase I in front of the generic sounding class name to distinguish interface classes from other normal classes that have specific purposes.

i.e. IAnimalType, ICarType, IClassType, IEmployeeType

Page 18: Lesson 8: The OOP concepts of Polymorphism and Interfaces

Fun facts about inheritance and implementations

A class can inherit a class and implement an interface

In ActionScript, a class can only inherit ONE superclass

In ActionScript, a class can implement multiple interfaces

An interface can inherit from another interface, though you're unlikely to need to do that