SENG 531: Labs TA: Brad Cossette Office Hours: Monday, Wednesday.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Written by: Dr. JJ Shepherd
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
Final Review.
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,
SENG 531: Labs TA: Brad Cossette Office Hours: Monday, Wednesday.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
SENG 531: Labs TA: Brad Cossette Office Hours: Monday, Wednesday.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
CPSC150 Abstract Classes Chapter 10. CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
CS1101: Programming Methodology Aaron Tan.
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.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
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.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
The Java Programming Language
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Exam 3 Preview.
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Written by: Dr. JJ Shepherd
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Inheritance ndex.html ndex.htmland “Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
ECE 264 Object-Oriented Software Development
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
BY:- TOPS Technologies
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
The Object-Oriented Thought Process Chapter 03
Inheritance and Polymorphism
Inheritance in Java.
Object Oriented Programming
CSC 143 Inheritance.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Extending Classes.
Java Programming Language
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Presentation transcript:

SENG 531: Labs TA: Brad Cossette Office Hours: Monday, Wednesday 3-4pm ICT 524

How the Labs Work Labs will cover  Course material in depth as needed  Assignments, Presentations, Final Review  General help with course work Labs are optional, but encouraged  Upcoming lab schedule posted on TA site

Labs This Week: Monday  Assignments Overview  Polymorphism in OO  Polymorphic Overloading Wednesday  AST’s  Single/Double Dispatch  The Visitor Pattern

Assignments: Stuff to Remember Groups allowed/encouraged Everyone must submit individual write-ups  The write-ups are worth more then actual code, so don’t blow them off! Short, Concise, & Quality= Happy T.A. Long, Wordy, & full of B.S.= Grumpy T.A.

Assignments: Assignment 1 Link: Key Points:  Determine all method invocations in the code AND  Determine what concrete methods are potentially invoked there Rob has suggested a starting form of your solution  Not required, but unless you have a better idea... Write-up – look at the questions posted on the assignment page

Assignments: Assignment 1 Decaff  no member, local, or anonymous types local = int x1, x2;  no arrays  no for statements  no exceptions, and so, no throw, throws, try/catch, etc.  no call chains allowed, e.g., a.b().c() (which means that a.b() would have to be assigned to a local variable)  no explicit constructor calls allowed, i.e., no this() or super()  no class selector Foo.class  no generics  no "on-demand" import statements import java.io.*

Assignments: Assignment 1 Coding style  Should mostly follow the Java Coding Standard   I also expect JavaDoc formatted comments Some recommended exceptions (IMHO):  Use all lowercase for variable names, _ to separate words  Use iii, jjj for loop iterators, not i,j.  Use super when invoking any parent methods/variables  Always use generics when possible  Never use reflection unless absolutely necessary

Polymorphism and OO Polymorphism:  Definition In more precise terms, polymorphism (object-oriented programming theory) is the ability of objects belonging to different types to respond to method calls of methods of the same name, each one according to an appropriate type-specific behaviour. The programmer (and the program) does not have to know the exact type of the object in advance, so this behavior can be implemented at run time (this is called late binding or dynamic binding).  Simpler Version Lets you treat derived class members just like their parent class's members. Source:

Polymorphism: So What? AbstractGrungeBandFooFightersNirvana > IRockBand

Polymorphism: Aggregation AbstractGrungeBand playLoudMusic()::void FooFighters singerOverdose()::void pumpOutAlbum()::void Nirvana singerOverdose()::void > IRockBand playMusic()::void This is how you’re probably used to thinking about Polymorphism in an OO context. The FooFighters class has all the functionality of the Nirvana class, perhaps even does a few things differently, and provides some new stuff.

Polymorphism: Sub-Typing AbstractGrungeBand playLoudMusic()::void FooFighters singerOverdose()::void pumpOutAlbum()::void Nirvana singerOverdose()::void > IRockBand playMusic()::void Look at this example: public class RockConcert { public void playAGig( AbstractGrungeBand band ) { band.playMusic(); band.playLoudMusic(); } We can pass either Nirvana or FooFighters objects as arguments to playMusic(). How hard would it to be to add new bands, or new rock music genres to the system?

Polymorphic Overloading Polymorphism  An object can be treated as having the type of any of its parents Overloading  More then one method with the same name, but accepting different types of arguments, exists

Are there situations where we want  function overloading, AND  calling a method using an object’s real sub- type, not its “current” sub-type? Polymorphic Overloading

Single Dispatch: Example Suppose we have the following classes: public class A { public String text = "This is A"; } public class A1 extends A { public String text_1 = "This is really A1"; } public class A2 extends A { public String text_2 = "This is really A2"; } Source:

Single Dispatch: Example And this is our Main class: public class Main { public void doIt( A object ) { System.out.println( object.text ); } public void doIt( A1 object ) { System.out.println( object.text_1 ); } public void doIt( A2 object ) { System.out.println( object.text_2 ); } public void foo( A object ) { doIt( object ); }

Single Dispatch: Example What happens when we execute: public static void main( String args[] ) { Main test = new Main(); test.doIt( new A() ); test.doIt( new A1() ); test.doIt( new A2() ); }

Single Dispatch: Example What happens when we execute: public static void main( String args[] ) { Main test = new Main(); test.doIt( new A() ); test.doIt( new A1() ); test.doIt( new A2() ); } This is A This is really A1 This is really A2

Single Dispatch: Example What happens when we execute: public static void main( String args[] ) { Main test = new Main(); test.foo( new A() ); test.foo( new A1() ); test.foo( new A2() ); }

Single Dispatch: Example What happens when we execute: public static void main( String args[] ) { Main test = new Main(); test.foo( new A() ); test.foo( new A1() ); test.foo( new A2() ); } This is A

Single Dispatch: Example What happens when we execute: public static void main( String args[] ) { Main test = new Main(); test.doIt( new A() ); test.doIt( ( A )( new A1() ) ); test.doIt( ( A )( new A2() ) ); } This is A

Single Dispatch: Example What happens when we execute: public static void main( String args[] ) { Main test = new Main(); test.doIt( new A() ); test.doIt( ( A )( new A1() ) ); test.doIt( ( A )( new A2() ) ); }

The crummy way to do this: Single Dispatch: Fix public class CrummyMain { public void foo( A object ) { if ( object instanceof A1 ) doIt( ( A1 )object ); else if ( object instanceof A2 ) doIt( ( A2 )object ); else doIt( object ); }

The elegant way to do this: Single Dispatch: Fix The Visitor Design Pattern

Extra slides...

public class PolymorphicOverloading { public static void main( String args[] ) { try { throw new FileNotFoundException( "Blah!" );} catch( Exception e ) { System.out.println( "We caught an exception" ); } catch( FileNotFoundException fnfe ) { System.out.println( "We caught a File Not Found Exception" ); } Polymorphic Overloading What’s the output from this code? Caveat: this isn’t really polymorphic overloading, but it’s a related example that gets the gist

public class PolymorphicOverloading { public static void main( String args[] ) { try { throw new FileNotFoundException( "Blah!" );} catch( Exception e ) { System.out.println( "We caught an exception" ); } catch( FileNotFoundException fnfe ) { System.out.println( "We caught a File Not Found Exception" ); } Polymorphic Overloading What’s the output from this code?

public class PolymorphicOverloading2 { public static void main( String args[] ) { try { throw new FileNotFoundException( "Blah!" ); } catch( FileNotFoundException fnfe ) { System.out.println( "We caught a File Not Found Exception" ); } catch( Exception e ) { System.out.println( "We caught an exception" ); } Polymorphic Overloading This is how a SANE person would have written that example: