Lecture 6 COMP1006/1406 (the OOP course) Summer 2014 M. Jason Hinek Carleton University
today s agenda assignments A1,A2,A3 are all marked A4 marking just started A5 is due Friday, A6 is due Monday a quick look back abstract things final things generics, interfaces, Comparable OOP and Java interfaces polymorphism inheritance 2
announcements Midterm 6:05-7:35pm, Wednesday July 30 UC 231 no aids class will resume at 8:00pm in the usual room All requests for accommodations must be received by noon on Friday, July 25 3
announcements Tutorials 6-8pm tutorial (no help for assignments) 8-9pm general help from TAs (including assignments) go to both if it will help if there is a seat you are welcome to attend (no homework help during first two hours) Office Hours Tuesday 4:00-6:00 in HP4155 (Sean) Thursday 11:00-12:30 in HP532 (Jason) for class material or Friday s assignment Thursday 5:00-6:00 in HP4155 (Connor) Friday 11:00-12:30 in HP5332 (Jason) for class material or Monday s assignment 4
last time... abstract classes cannot be instantiated no objects of the class can exist are valid reference types can have variables of this type (cannot use new and create an object of an abstract class) no restrictions on what can be in an abstract class attributes, methods, constructors does not need to have abstract methods cannot be final intention is for a descendant class to be concrete a concrete class can be instantiated (can use new and create an object of a concrete class) 5
last time... abstract methods have no body (definition) public abstract String getname(); forces class to also be abstract cannot be final intention is for a descendant class to define this method 6
last time... final final classes cannot be extended and cannot also be abstract final methods cannot be overriden and cannot also be abstract final attributes cannot change they must be defined (using initialization block or in constructor) the reference they store cannot be changed once defined 7
abstract classes why use abstract classes? abstract GameCharacters User abstract Game Friend Foe provides interface between implementation and users of code helps modularize code 8
let s take a break... for 5 minutes 9
interfaces Java interfaces contain method declarations (without definition) are public by default (you can omit this) implicitly abstract (you do not write this) contain constant attributes are public static final by default (can omit this) can extend any number of other interfaces are valid reference type but cannot be instantiated (like an abstract class) 10
interfaces public interface Flyable{ } int C = 299792458; boolean takeoff(); boolean fly(); boolean land(); provides a contract between code and users of code specifies behaviour does not specify state any class can implement this interface 11
abstract class vs interface abstract class valid reference data type not instantiable static and instance attributes method declarations and definitions defines identity of children (is-a) class extends 0,1 abstract class all subclasses are (very) related Java interface valid reference data type not instantiable public static final constants method declarations only can define identity (is-a) often defines abilities (-able) class implements n 0 interfaces independent classes implement same interface 12
abstract class vs interface abstract class valid reference data type not instantiable static and instance attributes method declarations and definitions defines identity of children (is-a) class extends 0,1 abstract class all subclasses are (very) related Java interface valid reference data type not instantiable public static final constants method declarations only can define identity (is-a) often defines abilities (-able) class implements n 0 interfaces independent classes implement same interface 12
data types 13
data types 13
generics allow us to create classes that uses any data type (non-primitive) public class Box<E>{ public E data; } public Box(E object){ this.data = object; } public class Box{ public Object data; } public Box(Object object){ this.data = object; } we could already do this by storing Objects this involves a lot of overhead checking types casting 14
generics allow us to create methods that use/input/return any data type (non-primitive) public static <T,K> T foo(int x, K[] k){ if( k.length > x ){ return new T(x); }else{ return new T(k[0].toString()); } } how could we do this with generics? 15
generics allow us to create methods that use/input/return any data type (non-primitive) public static <T,K> T foo(int x, K[] k){ if( k.length > x ){ return new T(x); }else{ return new T(k[0].toString()); } } how could we do this with generics? Java 8... (pass methods as input arguments) 15
generics generics can be a very useful tool in Java (and other languages) allows for generic algorithms eliminates casts provides stronger compile time type checks only work with Objects! (no primitive data types) 16
let s take a break... for 5 minutes 17
this again... this has two uses a reference to the current object used in instance methods cannot be used in static methods 18
this again... this has two uses a reference to the current object used in instance methods cannot be used in static methods used to call a constructor from within a constructor 18
this again... this has two uses a reference to the current object used in instance methods cannot be used in static methods used to call a constructor from within a constructor public Box(String name, int id){ // constructor this.name = name; this.id = id; } public Box(){ this("no name", -1); } public Box me(){ return this; } // constructor // method 18
initialization there are several ways to do initialization constructors (for objects) initialization blocks code gets added to constructors static initialization blocks 19
initialization there are several ways to do initialization constructors (for objects) initialization blocks code gets added to constructors static initialization blocks public class Box{ static String s1; String s2; static { System.out.println("static initialization block") } { } System.out.println("static initialization block") s2 = s1 + s1;... 19
inheritance access modifiers public class access, package access, subclass access, world access protected class access, package access, subclass access none (friendly) class access, package access private class access 20
inheritance access modifiers public class access, package access, subclass access, world access protected class access, package access, subclass access none (friendly) class access, package access private class access which modifier should you use? 20
for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. 21
for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. you will have noticed that this is also true for arrays sometimes too... 21
for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. you will have noticed that this is also true for arrays sometimes too... Java provides another for loop to make this easier 21
for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. you will have noticed that this is also true for arrays sometimes too... Java provides another for loop to make this easier for(type t: c) 21
for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. you will have noticed that this is also true for arrays sometimes too... Java provides another for loop to make this easier for(type t: c) for each Type t in c,... 21
for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. you will have noticed that this is also true for arrays sometimes too... Java provides another for loop to make this easier for(type t: c) for each Type t in c,... for(string s: list) 21
for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. you will have noticed that this is also true for arrays sometimes too... Java provides another for loop to make this easier for(type t: c) for each Type t in c,... for(string s: list) for each String s in list,... 21
for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. you will have noticed that this is also true for arrays sometimes too... Java provides another for loop to make this easier for(type t: c) for each Type t in c,... for(string s: list) for each String s in list,... for(arraylist<string> list: biglist) 21
for-each loops from the Oracle Java pages... Iterating over a collection is uglier than it needs to be. you will have noticed that this is also true for arrays sometimes too... Java provides another for loop to make this easier for(type t: c) for each Type t in c,... for(string s: list) for each String s in list,... for(arraylist<string> list: biglist) for each ArrayList<String> list in biglist,... 21
abstract data types an abstract data type or ADT is a collection of data a set of operations on that data possibly specified runtimes for the operation 22
let s take a break... for 5 minutes 23
some fundamental ADTs stack 24
some fundamental ADTs stack ordered collection of data (top is the last item added ) 24
some fundamental ADTs stack ordered collection of data (top is the last item added ) push (add an element to the top) pop (remove top element) peek (look at the top) isempty (is the stack empty) 24
some fundamental ADTs stack ordered collection of data (top is the last item added ) push (add an element to the top) pop (remove top element) Last In First Out (LIFO) peek (look at the top) isempty (is the stack empty) 24
some fundamental ADTs queue 25
some fundamental ADTs queue ordered collection of data (front and back) 25
some fundamental ADTs queue ordered collection of data (front and back) enqueue (add an element to the back) dequeue (remove element from the front) peek (look at the front) isempty (is the queue empty) 25
some fundamental ADTs queue ordered collection of data (front and back) enqueue (add an element to the back) dequeue (remove element from the front) First In First Out (FIFO) peek (look at the front) isempty (is the queue empty) 25
some fundamental ADTs stack and queue both are restricted lists stack removes items according to LIFO queue removes items according to FIFO 26
some fundamental ADTs stack and queue both are restricted lists stack removes items according to LIFO queue removes items according to FIFO Why do we need (or want) restricted lists? 26
some fundamental ADTs stack and queue both are restricted lists stack removes items according to LIFO queue removes items according to FIFO Why do we need (or want) restricted lists? forces the LIFO or FIFO principal might be more efficient 26
some fundamental ADTs stack and queue both are restricted lists stack removes items according to LIFO queue removes items according to FIFO Why do we need (or want) restricted lists? forces the LIFO or FIFO principal prevents intended misuse (cheating) prevents unintended misuse (mistakes) might be more efficient 26
some fundamental ADTs stack and queue both are restricted lists stack removes items according to LIFO queue removes items according to FIFO Why do we need (or want) restricted lists? forces the LIFO or FIFO principal prevents intended misuse (cheating) prevents unintended misuse (mistakes) might be more efficient array vs linked lists (we ll look at this soon) 26
some fundamental ADTs priority queue a queue in which removal is based on a priority (regardless of when added) highest priority item removed first emergency room in a hospital uses a priority queue deque a double-ended queue allows arbitrary adding/removing from front and back set an unordered collection of data add, remove, check for membership, size, etc 27
some fundamental ADTs map (or dictionary) 28
some fundamental ADTs map (or dictionary) a collection of (key,value) pairs keys are unique 28
some fundamental ADTs map (or dictionary) a collection of (key,value) pairs keys are unique mutable keys are dangerous some maps have order and some do not 28