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

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
SENG 531: Labs TA: Brad Cossette Office Hours: Monday, Wednesday.
What is Covered Everything up to and including Lecture 11 Types Recursion (including grammars) Lists and Trees GUIs Does not include Big-O notation (Lecture.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Trees. Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program, the.
SENG 531: Labs TA: Brad Cossette Office Hours: Monday, Wednesday.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Chapter 10: Inheritance and Polymorphism
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Abstract Classes and Interfaces
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Programming Languages and Paradigms Object-Oriented Programming.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Java Generics.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Programming in Java CSCI-2220 Object Oriented Programming.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
Lecture 4: Extending Classes. Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
CS Prelim Review – 10/15/09  First prelim is TOMORROW at 7:30 PM  Review session – Tonight 7:30 PM, Phillips 101  Things you should do:  Review every.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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
Syntax-Directed Definitions CS375 Compilers. UT-CS. 1.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Summary prepared by Kirk Scott
Polymorphism.
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Continuing Chapter 11 Inheritance and Polymorphism
ATS Application Programming: Java Programming
CSC 143 Inheritance.
Chapter 9 Inheritance and Polymorphism
Java Programming Language
Parsing & Scanning Lecture 2
Trees, part 2 Lecture 13 CS2110 – Spring 2019
Chapter 14 Abstract Classes and Interfaces
CIS 110: Introduction to computer programming
Presentation transcript:

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

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

Assignment 1 Questions You need to track each method invocation separately  e.g. don’t lump all moveBy() method invocations together You should figure out the type each method is invoked on as a means of distinguishing between potentially similar method names

Assignment 1 Questions Your output should be as informative as reasonably possible:  I should know which exactly which invocation/declaration you’re referring to at each point The info available in a single node is very limited  Explore what data is available to you...  There are ways to augment this info...

Generics Back in ye olden Java 1.4 days... public void oldPubCrawl( List pubs ) { for ( Iterator iii = pubs.iterator(); iii.hasNext(); ) { Bar current_bar = ( Bar )iii.next(); current_bar.drink(); current_bar.singVeryBadKaraoke(); }

Generics public void oldPubCrawl( List pubs ) { for ( Iterator iii = pubs.iterator(); iii.hasNext(); ) { Bar current_bar = ( Bar )iii.next(); We have to cast, because List will take any object Making List only take Bar objects means writing a specialized subclass

Generics Generics let us control what’s actually in a list… public void newestPubCrawl( List pubs ) { for ( Iterator iii = pubs.iterator(); iii.hasNext(); ) { Bar current_bar = iii.next(); current_bar.drink(); current_bar.singVeryBadKaraoke(); } …without having separate customized classes.

Generics A better way to write this: public void newestPubCrawl( List pubs ) { for ( Bar current_bar : pubs ) { current_bar.drink(); current_bar.singVeryBadKaraoke(); }

Abstract Syntax Trees (AST) Some compiler/language theory stuff:  Remember EBNF * form of context-free grammars? When parsing a grammar, the rules when applied to some inputs can create a tree If we ignore some syntactic symbols e.g. { }; etc. we can create an abstracted syntax tree... * Backus–Naur Form

Abstract Syntax Trees (AST) Example: public class Foo { public int bar = 0; } public class FooBar { public static void main(String args[]) { Foo fighter = new Foo(); fighter.bar = 6; System.out.print( fighter.bar ); } What could an AST for the outlined section look like?

Abstract Syntax Trees (AST) = fighterbar field access Foo basic block = declaration fighter 6 create object Foo Systemmethod invocation field access outprint argument fighterbar field access statement

Abstract Syntax Trees (AST) Key Points  You can recreate the source code from an AST  There’s not really an official standard for what an AST should look like This AST is different then the AST that Decaff will put out

AST: Why do you even care Well, it’s part of the assignment

AST: Why do you even care Well, it’s part of the assignment Which is easier to search in? Why?  Source Code vs. AST?  Specifically, what is easier to find in an AST? Program transformation (not in this course)

The Segueway: AST + Single Dispatch Suppose we have a tree of some sort: CPSC 331 – basic tree representation argument fighterbar field access TreeNode

The Segueway: AST + Single Dispatch Subclass TreeNode accordingly FieldReferenceObjectReference > ITreeNode FieldAccessAbstractArgument has Question: ITreeNode should probably be an Abstract class, not an Interface. Why?

AST Problem: Tree Traversal Basic algorithm is easy  Example AST uses an in-order traversal Others may use post-order BUT! At each node, you need:  Code to determine what the node is  Code to handle that node’s children

AST Problem: Tree Traversal Why won’t this work? public abstract class AbstractTreeNode { protected AbstractTreeNode parent = null; protected List children = null; protected String data = ""; }

AST Problem: Tree Traversal public class TreeTraversal { private AbstractTreeNode root_node = null; public TreeTraversal( AbstractTreeNode root ) { this.root_node = root; } public void traverse() { traverse( this.root_node ); } //Continued on Next Slide }

AST Problem: Tree Traversal public class TreeTraversal { //Continued protected void traverse( AbstractTreeNode node ) { if ( null != node.children && node.children.size() > 0 ) traverse( node.children.get( 0 ) ); System.out.println( "AbstractTreeNode: " + node.data ); if ( null != node.children && node.children.size() > 1 ) traverse( node.children.get( 1 ) ); } protected void traverse( Argument node ) { } protected void traverse( FieldAccess node ) { } protected void traverse( FieldReference node ) { } protected void traverse( ObjectReference node ) { } }

AST Problem: Tree Traversal Output:AbstractTreeNode: fighter AbstractTreeNode:. AbstractTreeNode: bar AbstractTreeNode: public static void main( String args[] ) { Argument root = new Argument( "" ); FieldAccess fa = new FieldAccess( "." ); root.children.add( fa ); ObjectReference or = new ObjectReference( "fighter" ); FieldReference fr = new FieldReference( "bar" ); fa.children.add( or ); fa.children.add( fr ); TreeTraversal printer = new TreeTraversal( root ); printer.traverse(); }

Tree Traversal + Single Dispatch So we have two problems:  We need a common supertype to support a tree that we can traverse without custom code  We still need to have specific subclasses at each node to reflect different data Java is single dispatch  How do we work around this?

Double Dispatch Single Dispatch  The type of an object is calculated at compile time Double Dispatch  The object’s type is determined at run-time  This is NOT duck-typing! What’s the difference?

Double Dispatch How do you simulated Double Dispatch?  Reflection object instanceof X Object.getClass() Are there problems with this?  The Visitor design pattern Simulate double dispatch by splitting responsibility

The Visitor Design Pattern Simulates Double Dispatch:  The Visitor class expects an interface that it can call to transfer control  The called class is responsible for calling the appropriate method on the Visitor

The Visitor Design Pattern The Visitor’s Target public interface class IVisitable { public void accept( AbstractVisitor visitor ); } The Visitor “gives itself” to the class it visits  This still lets us take advantage of the common superclass

The Visitor Design Pattern How to use the Visitor public class FieldAccess extends Argument implements IVisitable { //snip public void accept( AbstractVisitor visitor ) { left_child.accept( visitor ); visitor.visit( this ); //this = FieldAccess type object right_child.accept( visitor ); } Note that the call-back to the visitor now explicitly refers to the subclass type

The Visitor Design Pattern Writing the Visitor public abstract class AbstractVisitor { //snip public void visit( FieldAccess object ) { //do stuff } Now we can take advantage of polymorphic overloading, without Java’s single dispatch model being a problem.

Labs Next Week: Monday  Help with Assignment #1 Wednesday  Help with Assignment #1