Lesson 8: The OOP concepts of Polymorphism and Interfaces.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Component Patterns – Architecture and Applications with EJB copyright © 2001, MATHEMA AG Component Patterns Architecture and Applications with EJB JavaForum.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS 201 Functions Debzani Deb.
Object-oriented Programming Concepts
Abstract Classes and Interfaces
Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Object Oriented Software Development
Abstract classes and Interfaces. Abstract classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Lecture 9 Polymorphism Richard Gesick.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Lecture 21 Multiple Inheritance. What is Multiple Inheritance? We defined inheritance earlier in the semester as a relationship between classes. If class.
CS 320 Assignment 1 Rewriting the MISC Osystem class to support loading machine language programs at addresses other than 0 1.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
M1G Introduction to Programming 2 5. Completing the program.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Introduction to Object-Oriented Programming Lesson 2.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Review of Previous Classes Declaring Variables - var myVar:DataType = value Data Types – Number, uint, String, Boolean Functions – parameters, return.
Component Patterns – Architecture and Applications with EJB copyright © 2001, MATHEMA AG Component Patterns Architecture and Applications with EJB Markus.
Review of Previous Class Declaring variables var myVariableName:DataType = variableValue;
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Proxy. PBA WEB – BEWP 2 The Proxy pattern What is a Proxy? A Proxy is a ”placeholder” for a different object, to which we will not allow direct access.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
Chapter 5: Enhancing Classes
Reuse Separate classes could be developed for all the different objects that are needed in each program. However that would be wasteful. Often many functionalities.
Agenda Warmup AP Exam Review: Litvin A2
C++ Classes & Object Oriented Programming
Classes.
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Comp 249 Programming Methodology
Lecture 23 Polymorphism Richard Gesick.
Packages and Interfaces
Lesson 5: Building an App: Clicker Game
Advanced Java Programming
Java Inheritance.
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
Abstract Classes and Interfaces
Tonga Institute of Higher Education
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
CMSC 202 Exceptions.
„Lambda expressions, Optional”
C++ Object Oriented 1.
Presentation transcript:

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

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

Code demonstration Implement a new job - DELIVERY DRIVER And then reiterate the presentation from the beginning again to enforce concepts.

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

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.

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.

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

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.

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.

What are the uses of an interface? There are two uses for interfaces: Functional or architectural.

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.

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".

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.

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

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