Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders

Slides:



Advertisements
Similar presentations
Generics and the ArrayList Class
Advertisements

Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Today… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
Notices Assn 2 is due Friday, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including Thursday’s lecture: Big Topics.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
Inheritance and Polymorphism
Java Generics.
Chapter 20 Generic Classes and Methods
Agenda Warmup AP Exam Review: Litvin A2
A tree set Our SearchTree class is essentially a set.
CISC124 Labs start this week in JEFF 155: Meet your TA.
CMPE212 – Stuff… Assn 2 due next Friday. Winter 2018
CMPE212 – Stuff… Assn 3 due and Quiz 2 in the lab next week.
Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Object Oriented Programming
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Java Programming Language
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Quiz 1 marking nears completion!
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
A tree set Our SearchTree class is essentially a set.
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Generic programming in Java
CISC124 Assignment 4 on Inheritance due today at 7pm.
CISC124 Assignment 4 on Inheritance due next Friday.
CISC/CMPE320 - Prof. McLeod
Collections Not in our text.
CISC124 Assignment 4 on Inheritance due next Friday.
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
CISC/CMPE320 - Prof. McLeod
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,
CISC124 Assignment 3 sample solution will be posted tonight after 7pm.
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 sample solution is posted.
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 3 due next Friday.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders
CMPE212 – Reminders Course Web Site:
Chapter 8 Class Inheritance and Interfaces
CMPE212 – Reminders Quiz 1 marking underway. Assignment 1 due today.
Review: libraries and packages
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 2 due in a week.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Arrays.
Presentation transcript:

Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders Assignment 3 due today. Assignment 4 on Inheritance due in two weeks. Quiz 2 marking underway. Last Quiz in Week 11. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Today Continue Inheritance: Code example using our Person hierarchy. Winter 2019 CMPE212 5/3/2019 Today Continue Inheritance: Code example using our Person hierarchy. UML visualization. Start Generics: An example from the API – the ArrayList<T> class. Generic Classes in general – how to build them. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Aside - Multiple Inheritance? This is when a sub-class extends more than one super-class. In our diagrams, a sub-class would point to more than one super-class. Java does not support multiple inheritance. (C++ allows it.) The designers of Java felt that multiple inheritance would lead to structures that are too complex. Java designers have supplied the use of interfaces with multiple implementations as a way of getting around this restriction. Winter 2019 CMPE212 - Prof. McLeod

Aside - Multiple Inheritance?, Cont. A sneaky way to get around the lack of multiple inheritance in Java is to combine two classes into one, by making one of them an Inner Class, and then you extend the outer class... Winter 2019 CMPE212 - Prof. McLeod

Winter 2019 CMPE212 Code Example Before going further, let us look at expanded code for the simple Person/Student/Professor inheritance structure. Some features to look for: Use of ArrayList<T> Overridden methods Refined methods Use of instanceof keyword Use of super keyword Use of getClass() Use of abstract keyword in an abstract class Polymorphism!! Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Polymorphism, Cont. Where one type can appear as itself, but ends up being used as another type. Java is a very strongly typed language! Even so, you can allow one object to appear to be something else. In Java polymorphism must be constructed through object extension or interface implementation. Winter 2019 CMPE212 - Prof. McLeod

Polymorphism - Late and Early Binding Your program must always satisfy early binding for it to compile. Late binding occurs when the program is running, and only occurs when a variable of a parent-type object is pointing to a child object. The compiler does not (and cannot) check the late binding to see if it works. A failure of late binding results in a runtime error, not a compiler error. Winter 2019 CMPE212 - Prof. McLeod

Aside – Where to Check Arguments? Suppose the legal range of an argument depends on the type of the concrete object. But this attribute is declared in a base class, not in any of the concrete child classes. Where do you put the code to check the legality of this attribute and who will throw an exception? What is the order of operations? Is the attribute in the base class assigned before or after it is checked? Does this matter? Winter 2019 CMPE212 - Prof. McLeod

Visualization of Hierarchies The easiest way is to view the structure is as a UML (Unified Modeling Language) Class Diagram. This is what we have been doing, but normally more detail is shown. See our Person Hierarchy in a proper UML Class Diagram on the next slide: Winter 2019 CMPE212 - Prof. McLeod

Winter 2019 CMPE212 - Prof. McLeod

ArgoUML Eclipse can be configured to generate UML diagrams... It is better to use a stand-alone tool, such as ArgoUML, available from: http://argouml.tigris.org You can generate a diagram from existing code or even generate skeleton code from a diagram. Also, see “List of UML tools” in Wikipedia. Winter 2019 CMPE212 - Prof. McLeod

What’s Next? You have everything you need to work on Assignment 4. You are also ready to do Exercise 9. Next topic is Generics in Java: Start by looking at a Generic Collection type from the API – ArrayList<T>. This collection has been used in demo and assignment testing code already. Winter 2019 CMPE212 - Prof. McLeod

The ArrayList<T> Class Fall 2013 CMPE212 The ArrayList<T> Class This is a generic data structure class. The ArrayList<T> class resides in the java.util package. Stores and returns Objects of type T. (Referred to as “ArrayList<E>” in the API – same difference…) You can use the class without specifying a size. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

ArrayList<T> Class Syntax to create an ArrayList<T>: ArrayList<type> list_name = new ArrayList<type>(); type must be an Object, it cannot be a primitive type, but can be an array type. For example: ArrayList<Double> myList = new ArrayList<Double>(); You can supply an initial size to the constructor, if you wish. Winter 2019 CMPE212 - Prof. McLeod

ArrayList<T> Class, Cont. In newer versions of Java you do not have to specify the type twice. So, you can do: ArrayList<type> list_name = new ArrayList<>(); For example: ArrayList<Double> myList = new ArrayList<>(); The empty <> is called the “diamond”… This is an aspect of what is called “type inference”. Winter 2019 CMPE212 - Prof. McLeod

Using var With the ArrayList<T> Class You can declare an ArrayList collection using: var myList = new ArrayList<>(); myList can now hold any Object. If you want to restrict the element type, put a type in the diamond: var myList = new ArrayList<Integer>(); Winter 2019 CMPE212 - Prof. McLeod

ArrayList<T> Class, Cont. To add elements to the collection, for example: myList.add(Double.valueOf(456.78)) Or, myList.add(456.78); // Automatic Boxing You can also use .addAll(collection) to add a bunch of elements at once. To get the size of the collection, invoke the size() method: int myListSize = myList.size(); Winter 2019 CMPE212 - Prof. McLeod

ArrayList Class<T>, Cont. The get(index) method returns the element of type T at the given index. Note that index positions are numbered from zero (of course!). ( [ ] are not used!! ) The set(position, new_value) method changes the element at the given position. To insert an element, invoke add(position, new_value). All elements are moved up, and the new value is added at the given position. The method, remove(position) removes the element at the provided position. Also have removeAll(collection) Winter 2019 CMPE212 - Prof. McLeod

ArrayList Class<T>, Cont. If you are done adding elements to the ArrayList, invoke trimToSize() to remove empty element locations. Also, clone() does not work properly because it does not return an independent, un-aliased copy. The objects in the collection will still be aliased. Winter 2019 CMPE212 - Prof. McLeod

ArrayList Class<T>, Cont. toArray() returns the underlying array of type T[] as Object[] (which must be cast to T[]). Can also be invoked as toArray(tArray): Supply tArray which is an instantiated array of type T. The size will be increased if necessary, so just use a size of zero. For example, if T is String then invoke as in: arrayList.toArray(new String[0]) This method returns the array String[] of the exact size needed to hold all the elements in the arrayList. Winter 2019 CMPE212 - Prof. McLeod

ArrayList Class<T>, Cont. Other methods: contains(), containsAll(), indexOf(), removeIf(Predicate), removeRange(), subList(), sort() And, equals() is implemented in such a way that not only must the two collections be of the same List type and size, each pair of elements must also be equal. (So equals will have to be implemented properly for the element type as well). Winter 2019 CMPE212 - Prof. McLeod

ArrayList Class<T>, Cont. Once you have accessed an element of an ArrayList<T>, you do not have to cast it back to type T, it is already of the correct type. For example, to get the number back out of the first element in myList: double aVal = myList.get(0); Also uses automatic unboxing. This is an advantage over using a collection like Object[], for example where you would have to cast the returned Object. Winter 2019 CMPE212 - Prof. McLeod

ArrayList Class<T>, Cont. Final note: What is the underlying data structure for an ArrayList and how do you find out? Do you use the ArrayList<T> class for speed or for convenience? Winter 2019 CMPE212 - Prof. McLeod

Your Own Generic Classes Fall 2013 CMPE212 Your Own Generic Classes Also called “Parameterized Classes” - kind of like having a parameter in the class header - except it only specifies a type and does not pass a reference, like a parameter does. See the next slide for an example: Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

Your Own Generic Classes, Example public class Sample<T> { private T data; public void setData(T newData) { data = newData.clone(); } public T getData () { return data; } // end Sample<T> Winter 2019 CMPE212 - Prof. McLeod

Your Own Generic Classes, Example - Cont. Using the Sample generic class: Sample<Integer> num1 = new Sample<Integer>(); num1.setData(Integer.valueOf(45)); System.out.println(num1.getData().intValue()); Prints out 45. Winter 2019 CMPE212 - Prof. McLeod

Your Own Generic Classes, Example - Cont. Or, taking advantage of automatic boxing and un-boxing, as well as the “diamond”: Sample<Integer> num2 = new Sample<>(); num2.setData(67); System.out.println(num2.getData()); Prints out 67. Winter 2019 CMPE212 - Prof. McLeod

Your Own Generic Classes, Example - Cont. Any object type can be used for “T”: Sample<String> string1 = new Sample<>(); string1.setData("Hello!"); System.out.println(string1.getData()); Prints out Hello!. Winter 2019 CMPE212 - Prof. McLeod

Generic Classes, Limitations for T You can use an array type, so “<int[]>” would be legal. You cannot specify primitive types, so “<int>” would not be legal. Neither can you use Exception classes - (Why would you want to?) You can use interfaces and abstract types. Winter 2019 CMPE212 - Prof. McLeod

Your Own Generic Classes, Constructor A constructor for our Sample<T> class: public Sample (T newData) { data = newData.clone(); } Note that there is no use of <T> in the constructor. Winter 2019 CMPE212 - Prof. McLeod

Your Own Generic Classes, Constructor, Cont. Using the constructor and automatic boxing: Sample<Double> num3 = new Sample<>(56.7); System.out.println(num3.getData()); Winter 2019 CMPE212 - Prof. McLeod

Your Own Generic Classes, Notes You can specify any number of type parameters: public class Sample2<T1, T2, T3> {… Winter 2019 CMPE212 - Prof. McLeod

Your Own Generic Classes, Bounding You can also “bound” the parameters by specifying a root class: public class Sample3<T extends RootClass> So, only classes that extend RootClass, or RootClass itself, can be used for T, in this example. You can specify an upper bound for multiple classes and/or interfaces: public class Sample3<T extends A & B & C> Winter 2019 CMPE212 - Prof. McLeod

Your Own Generic Classes, Bounding The extends keyword allows you to specify an “upper” bound. You could use the “super” keyword to specify a “lower” bound: public class Sample3<T super Integer> In this case T could be Integer, Number or Object. Winter 2019 CMPE212 - Prof. McLeod

Your Own Generic Classes, Bounding Why bother? Upper bounding is like implementing an interface – it will ensure that the object T will have the methods in the class you are extending. For example <T extends Comparable<T>> will ensure that the type used for T will have a .compareTo() method. Winter 2019 CMPE212 - Prof. McLeod