Download presentation
Presentation is loading. Please wait.
Published byΠλειόνη Μάγκας Modified over 6 years ago
1
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Fall 2018 CISC124 11/23/2018 CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod
2
Today Inheritance, Cont. Start Generics in Java. Fall 2018
CISC124 - Prof. McLeod
3
Inheritance Code Example
Fall 2018 CISC124 Inheritance Code Example Look at the 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!! Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod
4
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. Fall 2018 CISC124 - Prof. McLeod
5
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. Fall 2018 CISC124 - Prof. McLeod
6
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? Fall 2018 CISC124 - Prof. McLeod
7
Aside – Preventing Instantiation
One technique that we have seen before is to make the single, default constructor private. But, suppose you must have a public constructor to be used by a child class to assign attributes. How can you prevent instantiation of the parent class in this case? For example, see the structure on the next slide: Fall 2018 CISC124 - Prof. McLeod
8
Aside – Preventing Instantiation, Cont.
Fastener Bolt CarriageBolt LagBolt Etc… Suppose you do not want the user of this code to be able to instantiate a Bolt. A generic “Bolt” just does not exist. Make the Bolt class abstract. Fall 2018 CISC124 - Prof. McLeod
9
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: Fall 2018 CISC124 - Prof. McLeod
10
Fall 2018 CISC124 - Prof. McLeod
11
ArgoUML Eclipse can be configured to generate UML diagrams...
It is better to use a stand-alone tool, such as ArgoUML, available from: You can generate a diagram from existing code or even generate skeleton code from a diagram. Also, see “List of UML tools” in Wikipedia. Fall 2018 CISC124 - Prof. McLeod
12
Hierarchy Respresentation in the API Docs
It would be nice to see class diagrams in the API Docs, but no such luck. See the Collections<T> hierarchy listing, for example. Fall 2018 CISC124 - Prof. McLeod
13
What’s Next? You have everything you need to work on Assignment 4.
You are also ready to do Exercise 10. 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. Fall 2018 CISC124 - Prof. McLeod
14
The ArrayList<T> Class
Fall 2013 CISC124 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. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod
15
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. Fall 2018 CISC124 - Prof. McLeod
16
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”. Fall 2018 CISC124 - Prof. McLeod
17
ArrayList<T> Class, Cont.
To add elements to the collection, for example: myList.add(new Double(456.78)); Or, myList.add(456.78); // Automatic Boxing To get the size of the collection, invoke the size() method: int myListSize = myList.size(); Fall 2018 CISC124 - Prof. McLeod
18
ArrayList Class<T>, Cont.
The get(index) method returns the Object at the given index. Note that index positions are numbered from zero (of course!). The set(position, new_value) method changes the Object 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. Fall 2018 CISC124 - Prof. McLeod
19
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. Fall 2018 CISC124 - Prof. McLeod
20
ArrayList Class<T>, Cont.
toArray() returns the underlying array of type 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. Fall 2018 CISC124 - Prof. McLeod
21
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. Fall 2018 CISC124 - Prof. McLeod
22
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? Fall 2018 CISC124 - Prof. McLeod
23
Your Own Generic Classes
Fall 2013 CISC124 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: Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod
24
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> Fall 2018 CISC124 - Prof. McLeod
25
Your Own Generic Classes, Example - Cont.
Using the Sample generic class: Sample<Integer> num1 = new Sample<Integer>(); num1.setData(new Integer(45)); System.out.println(num1.getData().intValue()); Prints out 45. Fall 2018 CISC124 - Prof. McLeod
26
Your Own Generic Classes, Example - Cont.
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. Fall 2018 CISC124 - Prof. McLeod
27
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!. Fall 2018 CISC124 - Prof. McLeod
28
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 interface and abstract types. Fall 2018 CISC124 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.