View
224
Download
0
Embed Size (px)
Citation preview
SENG 531: Labs
TA: Brad [email protected]
http://pages.cpsc.ucalgary.ca/~cossette/
Office Hours:Monday, Wednesday 3-4pm ICT 524
How the Labs Work
Labs will coverCourse material in depth as neededAssignments, Presentations, Final ReviewGeneral help with course work
Labs are optional, but encouragedUpcoming 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-upsThe 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: http://pages.cpsc.ucalgary.ca/~rwalker/teaching/seng531/W07/
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 http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html 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 VersionLets you treat derived class members just like their parent class's members.
Source: http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
Polymorphism: Aggregation
AbstractGrungeBand
playLoudMusic()::void
FooFighters
singerOverdose()::voidpumpOutAlbum()::void
Nirvana
singerOverdose()::void
<<interface>>IRockBand
playMusic()::void This is how you’re probably used to thinking about Polymorphism in an OOcontext.
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()::voidpumpOutAlbum()::void
Nirvana
singerOverdose()::void
<<interface>>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
PolymorphismAn object can be treated as having the type
of any of its parents
OverloadingMore then one method with the same name,
but accepting different types of arguments, exists
Are there situations where we want function overloading, ANDcalling 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:http://pages.cpsc.ucalgary.ca/~rwalker/teaching/seng531/W07/priv/OO%20Review.pdf
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 AThis is really A1This 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 AThis is AThis 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 AThis is AThis 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 );}
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: