COMP 401 INITIALIZATION AND INHERITANCE. Instructor: Prasun Dewan

Similar documents
COMP 401 ABSTRACT CLASSES. Instructor: Prasun Dewan

COMP 401 Prasun Dewan 1

COMP 401 Prasun Dewan 1

COMP 401 DYNAMIC DISPATCH AND ABSTRACT METHODS. Instructor: Prasun Dewan

Collections and Inheritance

COMP 110/401 COLLECTION KINDS. Instructor: Prasun Dewan

23. Generics and Adapters

COMP 401 INHERITANCE: IS-A. Instructor: Prasun Dewan

COMP 401 INHERITANCE. Instructor: Prasun Dewan

COMP 110 Prasun Dewan 1

COMP 401 INHERITANCE INHERITED VARIABLES AND CONSTRUCTORS. Instructor: Prasun Dewan

COMP 110/401 COLLECTION KINDS. Instructor: Prasun Dewan

Inheritance and Variables

Practice for Chapter 11

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

CS 251 Intermediate Programming Inheritance

CMSC 132: Object-Oriented Programming II

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

CS Programming I: Inheritance

CO Java SE 8: Fundamentals

Object Oriented Programming: Based on slides from Skrien Chapter 2

Introduction to Inheritance

Inheritance. Transitivity

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Overview of Java s Support for Polymorphism

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Java: introduction to object-oriented features

COMP 401 INHERITANCE: TYPE CHECKING. Instructor: Prasun Dewan

JAVA MOCK TEST JAVA MOCK TEST II

COMP 401: THE DUAL ROLE OF A CLASS. Instructor: Prasun Dewan (FB 150,

Introduction to Programming Using Java (98-388)

Rules and syntax for inheritance. The boring stuff

Logistics. Final Exam on Friday at 3pm in CHEM 102

Inheritance and Polymorphism

Inheritance. A key OOP concept

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Programming II (CS300)

Object Oriented Programming. Java-Lecture 11 Polymorphism

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Inheritance & Polymorphism

CS-202 Introduction to Object Oriented Programming

CS260 Intro to Java & Android 03.Java Language Basics

What is Inheritance?

Computer Science II (20073) Week 1: Review and Inheritance

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Class, Variable, Constructor, Object, Method Questions

CLASS DESIGN. Objectives MODULE 4

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

COMP 401 Prasun Dewan 1

CMSC 132: Object-Oriented Programming II

TeenCoder : Java Programming (ISBN )

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Language Features. 1. The primitive types int, double, and boolean are part of the AP

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Binghamton University. CS-140 Fall Dynamic Types

CompuScholar, Inc. 9th - 12th grades

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

C++ Important Questions with Answers

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Java Fundamentals (II)

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

A Short Summary of Javali

5/23/2015. Core Java Syllabus. VikRam ShaRma

Programming II (CS300)

QUESTIONS FOR AVERAGE BLOOMERS

OVERRIDING. 7/11/2015 Budditha Hettige 82

Inheritance -- Introduction

COMP 250 Fall inheritance Nov. 17, 2017

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

VIRTUAL FUNCTIONS Chapter 10

CSE 401/M501 Compilers

Chapter 14 Abstract Classes and Interfaces

Chapter 5 Object-Oriented Programming

PowerPoint Slides. Object-Oriented Design Using JAVA. Chapter 2. by Dale Skrien

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

ITI Introduction to Computing II

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Everything You Ever Wanted To Know About Java O-O. 7-Dec-15

Generic programming POLYMORPHISM 10/25/13

CH. 2 OBJECT-ORIENTED PROGRAMMING

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Implements vs. Extends When Defining a Class

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

Principles of Software Construction: Objects, Design, and Concurrency

Overview. Elements of Programming Languages. Objects. Self-Reference

Transcription:

COMP 401 INITIALIZATION AND INHERITANCE Instructor: Prasun Dewan

PREREQUISITE Inheritance Abstract Classes. 2

MORE INHERITANCE 3

AREGULARCOURSE public class ARegularCourse extends ACourse implements Course { int coursenum; public ARegularCourse (String thetitle, String thedept, int thecoursenum) { super (thetitle, thedept); coursenum = thecoursenum; public int getnumber() { return coursenum; 4

ALTERNATIVE AREGULARCOURSE public class ARegularCourse extends ACourse implements Course { int coursenum; public ARegularCourse (String thetitle, String thedept, int thecoursenum) { coursenum = thecoursenum; super (thetitle, thedept); public int getnumber() { return coursenum; Super call must be first statement in constructor but not other methods. Subclass may want to override initialization in super class Superclass vars initialized before subclass vars, which can use the former Subclass vars not visible in superclass 5

ALTERNATIVE AREGULARCOURSE public class ARegularCourse extends ACourse implements Course { int coursenum; public ARegularCourse (String thetitle, String thedept, int thecoursenum) { coursenum = thecoursenum; public int getnumber() { return coursenum; No super call()! 6

ACOURSE public abstract class ACourse extends Object { String title, dept; public ACourse (String thetitle, String thedept) { title = thetitle; dept = thedept; public String gettitle() { return title; public String getdepartment() { return dept; Missing super call Every class including Object has constructor 7

EQUIVALENT ACOURSE public abstract class ACourse extends Object { String title, dept; public ACourse (String thetitle, String thedept) { super(); title = thetitle; dept = thedept; public String gettitle() { return title; public String getdepartment() { return dept; Automatically inserted 8

ALTERNATIVE AREGULARCOURSE public class ARegularCourse extends ACourse implements Course { int coursenum; public ARegularCourse (String thetitle, String thedept, int thecoursenum) { coursenum = thecoursenum; public int getnumber() { return coursenum; No super call()! 9

ALTERNATIVE AREGULARCOURSE public class ARegularCourse extends ACourse implements Course { int coursenum; public ARegularCourse (String thetitle, String thedept, int thecoursenum) { super(); coursenum = thecoursenum; public int getnumber() { return coursenum; Automatically inserted Java complains superclass does not have parameterless constructor 10

ADVANCED INITIALIZATION 11

ABSTRACT METHODS IN CONSTRUCTORS public abstract class ACourse implements Course { String title, dept; public ACourse (String thetitle, String thedept) { title = thetitle; dept = thedept; // innocuous debugging statement added to ACourse System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()) ; public String gettitle() { return title; public String getdepartment() { return dept; abstract public int getnumber(); 12

ABSTRACT METHODS IN CONSTRUCTORS getnumber() called before subclass constructor in ARegularCourse has initialized variables. 13

ABSTRACT METHODS IN CONSTRUCTORS Beware of abstract methods being called in constructors. They may access uninitialized variables in subclasses! 14

INITIALIZING DECLARATION public class ARegularCourse extends ACourse { int coursenum = 99; public ARegularCourse (String thetitle, String thedept, int thecoursenum) { super (thetitle, thedept); coursenum = thecoursenum; public int getnumber() { return coursenum; 15

INITIALIZING DECLARATIONS Initializations in declarations processed after superclass constructor returns Try stepping into a constructor in a debugger. Why correct value? 16

CONSTANT INITIALIZATIONS Done when constants allocated memory. (Should be) shared by all instances. When class is loaded in memory. Not when an instance created. 17

PROCESSING OF NEW (new ARegularCourse ("Intro. Prog.", "COMP", 14)); The variables declared in ARegularCourse and Course are allocated space but not initialized. The constructor of ARegularCourse is called. It calls the constructor of ACourse in its first statement. 18

PROCESSING OF NEW The initializations in the declarations of the variables of ACourse are processed. In this case there are no initializations. The constructor of ACourse is started. The two instance variables of ACourse are assigned parameter values, Intro. Prog. And COMP. The values of these variables are printed. The method getnumber() of ARegularCourse is called and the default value of coursenum is printed. 19

PROCESSING OF NEW The constructor of ACourse returns. The initializations in the declarations of the variables of ARegularCourse are processed. In this case, coursenum is assigned the value 0. Execution resumes in the constructor of ARegularCourse.The coursenum variable is assigned the parameter value, 14. The constructor of ARegularCourse returns. The new statement completes, returning the new instance to its caller. 20

MULTIPLE CONSTRUCTORS public abstract class ACourse implements Course { String title = COMP ; String dept = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()); public ACourse () { System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()); public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); How to remove code duplication? 21

CONSTRUCTOR CALLING CONSTRUCTOR public abstract class ACourse implements Course { String title = COMP ; String dept = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()); public ACourse () { this (title, dept); public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); This implies constructor of same class 22

EQUIVALENT CODE public abstract class ACourse implements Course { String title = COMP ; String dept = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { super(); title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()); public ACourse () { this (title, dept); No super public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); Super may have side effects, so cannot call super() before this() Java complains that instance variables not initialized 23

CONSTRUCTOR CALLING CONSTRUCTOR public abstract class ACourse implements Course { String title = COMP ; String dept = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { title = thetitle; dept = thedept; this(); public ACourse () { System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()); public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); Java complains that this() must be first call, as it results in super() being called 24

CONSTRUCTOR CALLING CONSTRUCTOR public abstract class ACourse implements Course { String title, dept; final String DEFAULT_DEPT = COMP ; final String DEFAULT_TITLE = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()); public ACourse () { this ( COMP, Topics in Computer Science ); public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); No complaints. 25

CONSTRUCTOR CALLING CONSTRUCTOR ; public abstract class ACourse implements Course { String title, dept; final String DEFAULT_DEPT = COMP ; final String DEFAULT_TITLE = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()); public ACourse () { this (DEFAULT_DEPT, DEFAULT_TITLE ); public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); Java complains that instance vars cannot be accessed. 26

POSSIBLE CONSTRUCTOR CALL SEMANTICS Rule so far: each constructor calls superclass constructor. Problem: then super class initialized multiple times. Not a problem if each initialization yields the same result. Can have multiple constructors doing different initializations. Same constructor may do different things on different invocations or may have side effects. 27

ACTUAL SEMANTICS A constructor may call another constructor as first statement. Super not called in that case. Only last call in constructor chain calls super. Hence no danger of super being initialized multiple times. But danger that constructor parameters may be uninitialized. Literal parameters ok. Constructor call must be first statement. Otherwise code executed before superclass initialized 28

SOLUTION: INIT METHOD ; public abstract class ACourse implements Course { String title, dept; final String DEFAULT_DEPT = COMP ; final String DEFAULT_TITLE = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { init (thetitle, thedept) public ACourse () { init (DEFAULT_DEPT, DEFAULT_TITLE); void init (String thetitle, String thedept) { title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()); public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); 29

ALLOWING INIT AFTER CONSTRUCTION ; public abstract class ACourse implements Course { String title = Topics in Computer Science ;; String dept = COMP ; public ACourse (String thetitle, String thedept) { init (thetitle, thedept) public ACourse () { init (DEFAULT_DEPT, DEFAULT_TITLE); public void init (String thetitle, String thedept) { title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()); public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); 30

AREGULARCOURSE ; public class ARegularCourse extends ACourse { int coursenum; public ARegularCourse (String thetitle, String thedept, int thecoursenum) { super (thetitle, thedept); coursenum = thecoursenum; public ARegularCourse () { public int getnumber() { return coursenum; coursenum uninitialized, as init method only in super class Course course =new ARegularCourse(); course.init( Meaning of Life, PHIL ); 31

AREGULARCOURSE WITH INIT ; public class ARegularCourse extends ACourse { int coursenum; public ARegularCourse (String thetitle, String thedept, int thecoursenum) { super (thetitle, thedept); coursenum = thecoursenum; public ARegularCourse () { public int getnumber() { return coursenum; public void init (String thetitle, String thedept, int thecoursenum) { coursenum = thecoursenum; Super class init not called Course course =new ARegularCourse(); Course.init( Meaning of Life, PHIL, 999); 32

AREGULARCOURSE WITH INIT ; public class ARegularCourse extends ACourse { int coursenum; public ARegularCourse (String thetitle, String thedept, int thecoursenum) { super (thetitle, thedept); coursenum = thecoursenum; public ARegularCourse () { public int getnumber() { return coursenum; public void init (String thetitle, String thedept, int thecoursenum) { init(thetitle, thecoursenum); coursenum = thecoursenum; Course course =new ARegularCourse(); Course.init( Meaning of Life, PHIL, 999); Super class init called. 33

TWO WAYS TO CONSTRUCT Initialized construction new ARegularCourse ( Intro. Prog, COMP, 14); Construction and then initialization (new ARegularCourse()).init( Intro Prog., COMP, 14) 34

PARAMETERIZED CONSTRUCTORS Can create multiple parameterized constructors To initializes some subset of instance variables. Use default values assigned by initializing declarations for the rest. public ACourse (String thetitle) { title = thetitle; Add an init method for each parameterized constructor 35

INIT METHODS Allows initialization after object is created. Initializer can be different from creator Abstract class may initialize Concrete factory method may instantiate. Init methods can be in interfaces Init method(s) recommended but not required 36

AFRESHMANSEMINAR public class AFreshmanSeminar extends ACourse { public AFreshmanSeminar (String thetitle, String thedept) { super (thetitle, thedept); title = thetitle; public int getnumber() { return SEMINAR_NUMBER; Allowed since in same package 37

ABSTRACT COURSE ; public abstract class ACourse { String title, dept; public ACourse (String thetitle, String thedept) { //title = thetitle; dept = thedept; // innocuous debugging statement added to ACourse System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber() public String gettitle() { return title; public String getdepartment() { return dept; abstract public int getnumber(); No longer necessary 38

DISPLAY ABSTRACT COURSE 39

ABSTRACT COURSE ; public abstract class ACourse { String title, dept; public ACourse (String thetitle, String thedept) { //title = thetitle; dept = thedept; // innocuous debugging statement added to ACourse System.out.println("New course created: " + "Title:" + title+ " Dept:"+ dept + " Number: " + getnumber() public String gettitle() { return title; public String getdepartment() { return dept; Accesses Uninitialized variable 40

REDEFINING SUPERCLASS VARIABLES ; public class ARegularCourse extends ACourse { int coursenum; String title; public ARegularCourse (String thetitle, String thedept, int thecoursenum) { super (thetitle, thedept); coursenum = thecoursenum; title = thetitle; public int getnumber() { return coursenum; Refers to overriding variable Overriding variable legal in Java 41

ALTERNATIVE AFRESHMANSEMINAR ; public class AFreshmanSeminar extends ACourse { String title; public AFreshmanSeminar (String thetitle, String thedept) { super (thetitle, thedept); title = thetitle; public int getnumber() { return SEMINAR_NUMBER; 42

REDECLARING SUPERCLASS VARIABLES 43

ABSTRACT COURSE ; public abstract class ACourse implements Course { String title, dept; public ACourse (String thetitle, String thedept) { //title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber() public String gettitle() { return title; public String getdepartment() { return dept; abstract public int getnumber(); Equals accesses uninitialized variable 44

RE-DECLARING SUBCLASS VARIABLES Happens accidentally when class is re-factored manually to move subclass variables to superclasses. Original variable in subclass remains. Beware! Use Eclipse refactor move command when possible. 45

EXTRA SLIDES 46

CANNOT ALWAYS GET RID OF MANUAL DISPATCH/INSTANCOF static void print (Course course) { if (course instanceof ARegularCourse) printheader ((ARegularCourse) course); else if (course instanceof AFreshmanSeminar)) printheader ((AFreshmanSeminar) course); System.out.println( course.gettitle() + " " + course.getdepartment() + course.getnumbert() ); static void printheader (ARegularCourse course) { System.out.print( Regular Course: ); static void printheader (AFreshmanSeminar course) { System.out.print( Freshman Seminar: ); 47

MULTIPLE INITS ; public abstract class ACourse implements Course { String title = Topics in Computer Science ;; String dept = COMP ; public ACourse (String thetitle, String thedept) { init (thetitle, thedept); init(); public ACourse () { init (); public void init (String thetitle, String thedept) { title = thetitle; dept = thedept; void init() { System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()); public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); 48

SOME SUPERTYPES IN EXAMPLES Abstract AStringHistory ACourse AStringDatabase AFreshmanSeminar ARegularCourse AStringSet 49

ABSTRACT CLASSES IN JAVA.UTIL? Collection Set List HashSet Vector ArrayList LinkedList implements extends 50

ABSTRACT CLASSES IN JAVA.UTIL AbstractCollection AbstractSet AbstractList Collection AbstractSequentialList Set List HashSet Vector ArrayList LinkedList implements extends 51

POLYMORPHISM VS. OVERLOADING AND DYNAMIC DISPATCH Create polymorphic code when you can In overloading and dynamic dispatch, multiple implementations associated with each method name. In polymorphic case, single implementation. Use interfaces rather than classes as types of parameters Use supertypes rather than subtypes as types of parameters Polymorphism vs. Overloading Polymorphism: single print (Course course) Overloading: print (ARegularCourse course) and print (AFreshmanSeminar course) with same implementation. Polymorphism vs. Dynamic Dispatch Polymorphism: single gettitle() in ACourse Dynamic dispatch: gettitle() in AFreshmanSeminar() and gettitle() in ARegularCourse() with same implementation. 52

POLYMORPHISM VS. OVERLOADING AND DYNAMIC DISPATCH Cannot always create polymorphic method. getnumber() for ARegularCourse and AFreshmanSeminar do different things. print(course course) and print (CourseList courselist) do different things. When polymorphism not possible try overloading and dynamic dispatch. 53

OVERLOADING VS. DYNAMIC DISPATCH Overloading: Object is parameter Dynamic dispatch: Object is target Method in object class vs. external class Program decomposition issue print(course), print (CourseList) definitions overloaded AFreshmanSeminar.getCourseNumber(), ARegularCourse.getCourseNumber() dynamically dispatched Overload resolution looks at multiple parameter types More general and used for that reason also Dynamic dispatch is done at runtime Can be used for that reason 54

size array size array 43 James Dean 42 James Dean Joe Doe Jane Smith Jane Smith John Smith John Smith 55

array array Current window James Dean Joe Doe Jane Smith Current window James Dean Joe Doe Jane Smith John Smith John Smith 56

size array size array size array 43 James Dean 40 James Dean 41 Jane Smith Joe Doe Jane Smith John Smith Joe Doe Jane Smith John Smith Joe Doe Jane Smith John Smith 57

PHYSICAL AND COMPUTER INHERITANCE Physical Object Implicit Extension Object Animal Rock AString History String Mammal AString Database Primate Human 58

DIFFERENT TYPES public void addelement(object element) { if (isfull()) System.out.println("Adding item to a full history"); else { contents[size] = element; size++; public void addelement(string element) { if (member(element)) return; super.addelement(element); Signature of Overridden method should be identical in overriding class, otherwise considered different method 59

== FOR OBJECTS Point p1 = new ACartesianPoint(200, 200); ACartesianPoint@8 8 200 200 Point p1 16 8 Point p2 = p1; p2 == p2 P1 true P2 Point p2 48 8 ACartesianPoint@8 But not necessary. 60

ABSTRACT COURSE ; public abstract class ACourse { String title, dept; public ACourse (String thetitle, String thedept) { super(); title = thetitle; dept = thedept; public String gettitle() { return title; public String getdepartment() { return dept; Does not implement an interface 61

ANOTHER COURSE ; public abstract class ACourse implements Course { String title, dept; public ACourse (String thetitle, String thedept) { super(); ; title = thetitle; public interface Course { dept = thedept; public String gettitle() { return title; public String getdepartment() { return dept; abstract public int getnumber(); public String gettitle(); public String getdepartment(); public int getnumber(); Abstract Method 62

ABSTRACT METHOD Declared only in abstract classes Keyword: abstract No body Each (direct or indirect) subclass must implement abstract methods defined by an abstract class. Much like each class must implement the methods defined by its interface(s). 63

COURSE DISPLAYER USER INTERFACE 64

MAIN CLASS: FILLING LIST static CourseList courses = new ACourseList(); static void fillcourses() { courses.addelement(new ARegularCourse ("Intro. Prog.", "COMP", 14)); courses.addelement(new ARegularCourse ("Found. of Prog.", "COMP", 114)); courses.addelement(new AFreshmanSeminar("Comp. Animation", "COMP")); courses.addelement(new AFreshmanSeminar("Lego Robots", "COMP")); 65

JAVA VECTORS, ARRAY LISTS AND ITERATORS Just like collections we defined Except they can store and iterate arbitrary objects 66

VECTORS (JAVA.UTIL.VECTOR) Vector v; null Vector Instance James Dean Joe Doe Object Point Instance Vector Instance v = new Vector(); v.addelement( James Dean ) v.addelement( Joe Doe ) v.addelement(5) v.addelement(new ACartesianPoint(5, 5)) v.addelement(new Vector()) 67

IMPORTANT METHODS OF CLASS VECTOR public final int size() public final Object elementat(int index) public final void addelement(object obj) public final void setelementat(object obj, int index) public final void insertelementat(object obj, int index) public final boolean removeelement(object obj) public final void removeelementat(int index) public final int indexof(object obj) public final Enumeration elements() 68

METHODS OF INTERFACE ENUMERATION (JAVA.UTIL.ENUMERATION) public boolean hasmoreelements(); public Object nextelement(); Enumeration elements = vector.elements(); while ( elements.hasmoreelements()) System.out.println(elements.nextElement(); 69

METHODS OF INTERFACE ENUMERATION (JAVA.UTIL.ENUMERATION) public boolean hasmoreelements(); public Object nextelement(); for (Enumeration elements = vector.elements(); elements.hasmoreelements();) System.out.println(elements.nextElement(); 70

USING VECTOR DIRECTLY String history user Vector v = new Vector(); Vector null Instance size(): int PointModel Instance PointModel Instance Vector Instance v.addelemen( Joe Doe ); elementat(): index Object addelement(): Object void removeelementat (): Object void Violating Least Privilege v.addelement( John Smith ); v.addelement(new Vector()); v.removeelementat(0); 71

ENCAPSULATING VECTOR String history user StringHistory stringhistory = new AStringHistory() AStringHistory instance Vector v = new Vector(); size(): int Vector null Instance PointModel Instance size(): int elementat(): index Object addelement(): Object void elementat(): index String addelement(): String void removeelementat (): Object void stringhistory.addelement( Joe Doe ); stringhistory.addelement(new Vector()) 72

ENCAPSULATING VECTOR import java.util.vector; public class AStringHistory implements StringHistory { Vector contents = new Vector(); public void addelement (String s) { contents.addelement(s); public String elementat (int index) { return (String) contents.elementat(index); public int size() { return contents.size(); Simply converts types 73

ADAPTER PATTERN client adapter adaptee Degree of adaptation undefined. Methods offered to client Adapted name Adapted type 74

ADAPTER PATTERN client adapter adaptee Degree of adaptation undefined. Methods offered to client Adapted name Adapted type 75

PARSER STRUCTURE Each production associated with a parser method. Parser method returns object associated with LHS of production. Usually at start of method execution Prefix of unconsumed input should be legal phrase derived from LHS Unless calling method consumed one ore more tokens of the phrase to choose an alternative rule Such a parser called: recursive descent parser Illustrates top-down programming 76

77

ALTERNATIVE AREGULARCOURSE ; public class ARegularCourse extends ACourse { int coursenum; public ARegularCourse (String thetitle, String thedept, int thecoursenum) { super (thetitle, thedept); coursenum = thecoursenum; public int getnumber() { return coursenum; Implementation of abstract method Implicitly implements interfaces implemented by superclass 78

ALTERNATIVE AFRESHMANSEMINAR ; public class AFreshmanSeminar extends ACourse { public AFreshmanSeminar (String thetitle, String thedept) { super (thetitle, thedept); public int getnumber() { return SEMINAR_NUMBER; Implementation of abstract method Implicitly implements interfaces implemented by superclass 79

ALTERNATIVE AFRESHMANSEMINAR ; public class AFreshmanSeminar extends ACourse { public AFreshmanSeminar (String thetitle, String thedept) { super (thetitle, thedept); title = thetitle; public AFreshmanSeminar () { public int getnumber() { return SEMINAR_NUMBER; new AFreshmanSeminar ( Lego Robots, COMP ); new AFreshmanSeminar(); 80

MULTIPLE CONSTRUCTORS ; public abstract class ACourse implements Course { String title, dept; final String DEFAULT_DEPT = COMP ; final String DEFAULT_TITLE = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber() public ACourse () { dept = DEFAULT_DEPT; title = DEFAULT_TITLE; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()) ; public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); 81

CODE DUPLICATION ; public abstract class ACourse implements Course { String title, dept; final String DEFAULT_DEPT = COMP ; final String DEFAULT_TITLE = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber() public ACourse () { dept = DEFAULT_DEPT; title = DEFAULT_TITLE; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()) ; public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); How to remove code duplication? 82

CONSTRUCTOR CALLING CONSTRUCTOR ; public abstract class ACourse implements Course { String title, dept; final String DEFAULT_DEPT = COMP ; final String DEFAULT_TITLE = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()); public ACourse () { this (DEFAULT_DEPT, DEFAULT_TITLE ); public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); this implies constructor of same class. 83

EQUIVALENT CODE ; public abstract class ACourse implements Course { String title, dept; final String DEFAULT_DEPT = COMP ; final String DEFAULT_TITLE = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { super(); title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()); super()? public ACourse () { this (DEFAULT_DEPT, DEFAULT_TITLE ); public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); Super may have side effects, so cannot call super() before this() 84

CONSTRUCTOR CALLING CONSTRUCTOR ; public abstract class ACourse implements Course { String title, dept; final String DEFAULT_DEPT = COMP ; final String DEFAULT_TITLE = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()); public ACourse () { this (DEFAULT_DEPT, DEFAULT_TITLE ); public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); Java complains that instance vars cannot be accessed. 85

MULTIPLE CONSTRUCTORS ; public abstract class ACourse implements Course { String title, dept; final String DEFAULT_DEPT = COMP ; final String DEFAULT_TITLE = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber() public ACourse () { dept = DEFAULT_DEPT; title = DEFAULT_TITLE; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()) ; public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); 86

CODE DUPLICATION ; public abstract class ACourse implements Course { String title, dept; final String DEFAULT_DEPT = COMP ; final String DEFAULT_TITLE = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber() public ACourse () { dept = DEFAULT_DEPT; title = DEFAULT_TITLE; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()) ; public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); How to remove code duplication? 87

CONSTRUCTOR CALLING CONSTRUCTOR ; public abstract class ACourse implements Course { String title, dept; final String DEFAULT_DEPT = COMP ; final String DEFAULT_TITLE = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()); public ACourse () { this (DEFAULT_DEPT, DEFAULT_TITLE ); public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); this implies constructor of same class. 88

EQUIVALENT CODE ; public abstract class ACourse implements Course { String title, dept; final String DEFAULT_DEPT = COMP ; final String DEFAULT_TITLE = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { super(); title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()); super()? public ACourse () { this (DEFAULT_DEPT, DEFAULT_TITLE ); public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); Super may have side effects, so cannot call super() before this() 89

CONSTRUCTOR CALLING CONSTRUCTOR ; public abstract class ACourse implements Course { String title, dept; final String DEFAULT_DEPT = COMP ; final String DEFAULT_TITLE = Topics in Computer Science ; public ACourse (String thetitle, String thedept) { title = thetitle; dept = thedept; System.out.println("New course created: " + "Title:" + title + " Dept:"+ dept + " Number: " + getnumber()); public ACourse () { this (DEFAULT_DEPT, DEFAULT_TITLE ); public String gettitle() {return title; public String getdepartment() {return dept; abstract public int getnumber(); Java complains that instance vars cannot be accessed. 90

ABSTRACT METHOD EXAMPLE ; public abstract class ACourse { String title, dept; public ACourse (String thetitle, String thedept) { super(); title = thetitle; dept = thedept; public String gettitle() { return title; public String getdepartment() { return dept; abstract public int getnumber(); Abstract Method 91

FACTORY ABSTRACT METHOD abstract <T> <M>( ) ; extends <T> <M>( ) { return new <C1> ( ) <T> <M> ( ){ return new <C2> ( ) Abstract method that returns an instance of some type T like a factory, it creates and initializes an object. 92

INIT METHODS Allows initialization after object is created. Initializer can be different from creator Init methods can be in interfaces Init method(s) recommended but not required 93

extends VISIBILITY OF SUPERCLASS MEMBERS AFreshmanSeminar getnumber() extends title dept ACourse gettitle() getdepartment() Should non public identifiers be visible in subclasses? coursenum ARegularCourse getnumber() 94

ACCESS CONTROL ON VARIABLES AND INHERITANCE, PACKAGES public: accessible in all classes. protected: accessible in all subclasses of its class and all classes in its package. default: accessible in all classes in its package. private: accessible only in its class. 95

ACCESSING SUPERCLASS VARIABLES ; public class ARegularCourse extends ACourse { int coursenum; public ARegularCourse (String thetitle, String thedept, int thecoursenum) { super (thetitle, thedept); coursenum = thecoursenum; title = thetitle; public int getnumber() { return coursenum; Access allowed since in same package 96