Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC124 Assignment 4 on Inheritance due today at 7pm.

Similar presentations


Presentation on theme: "CISC124 Assignment 4 on Inheritance due today at 7pm."— Presentation transcript:

1 CISC124 Assignment 4 on Inheritance due today at 7pm.
Fall 2018 CISC124 12/30/2018 CISC124 Assignment 4 on Inheritance due today at 7pm. Last Quiz next week (to avoid having it in week 12…). More info to follow. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

2 Today Generics in Java: Generic or Object? Wildcards.
How to Build a Generic Factory Method. If we have time: Start Lambda Functions. Fall 2018 CISC124 - Prof. McLeod

3 Your Own Generic Methods
Fall 2013 CISC124 Your Own Generic Methods For example in the class Utility: public static <T> T getMidpoint(T[] a) { return a[a.length/2]; } To invoke: String mid = Utility.<String>getMidpoint(b); // b is some array of String’s Or: String mid = Utility.getMidpoint(b); Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

4 Your Own Generic Methods, Cont.
How else could you write getMidpoint? public static Object getOMidpoint(Object[] objArray) { return objArray[objArray.length / 2]; } // end getOMidpoint See Utility.java and TestUtility.java. Are generic methods better? Fall 2018 CISC124 - Prof. McLeod

5 Why Generic Methods? In the example (strings is of type String[]):
String mid = (String)Utility.getOMidpoint(strings); Suppose you wrote the following by mistake: Integer midI = (Integer)Utility.getOMidpoint(strings); You will not get a compilation error, but a “ClassCastException” will crash your program! Fall 2018 CISC124 - Prof. McLeod

6 Why Generic Methods?, Cont.
On the other hand: Integer midI = Utility.<String>getMidpoint(b); Gives you a compilation error and will not run. The compiler is enforcing type safety, which it cannot do when you are just using Objects. This advantage applies to any generic method or class. Fall 2018 CISC124 - Prof. McLeod

7 Polymorphism Demo, Again
See a method that is equivalent to the one using DoRunRun[] as a parameter type, but is now generic. The generic version does not show any advantage over the one using DoRunRun[], but both of these are better than the one using Object[]! Fall 2018 CISC124 - Prof. McLeod

8 Aside - Backwards Compatibility
Generic code can make for very versatile programs that can be applied to a range of objects – saving you a great deal of boring coding work. But, generic code will not work easily with pre-Java 5.0 code. And, of course, the Java 7 type inference stunts will not work with pre-Java 7.0 code. Generics are a bit like Templates in C++ and C#, but are implemented in a different way. Fall 2018 CISC124 - Prof. McLeod

9 Generic Wildcards If Child is a subclass of Parent then this works:
Parent obj = new Child(); But this will not work: ArrayList<Parent> alObj = ArrayList<Child>(); This will work: ArrayList<?> alObj = ArrayList<Child>(); A “wildcard” is the ? within the < >. A wildcard can provide a super type generic class for other generic classes. Fall 2018 CISC124 - Prof. McLeod

10 Generic Wildcards, Cont.
You can bind a wildcard to classes that extend other classes using the extends keyword. For example: ArrayList<? extends Person> db db can be assigned to an object of type ArrayList<Person>, ArrayList<Student> or ArrayList<Professor>. Fall 2018 CISC124 - Prof. McLeod

11 Generic Wildcards, Cont.
However, a generic typed with a wildcard is not mutable. You can get around this using generic methods without the wildcard. But: This is more restrictive and potentially “brittle”. Avoid casting using the generic type. This may lead to “Unchecked Casts” which reduces the type safety of the generic method. (Hint: Use of Class<T> may be a way around this problem. Fall 2018 CISC124 - Prof. McLeod

12 Generic Wildcards, Cont.
ArrayList<?>[] is the only possible type that allows the creation of an array of generic classes. But, why would you want to? Easier to create an ArrayList of ArrayLists, for example. Fall 2018 CISC124 - Prof. McLeod

13 Wildcards Demo TestWildcards.java
Uses the Person hierarchy from last week. Plays with wildcards, bounded generic methods, and how to create an array of generic classes. Fall 2018 CISC124 - Prof. McLeod

14 Aside – Lower Bound for Wildcard
In ? extends T the extends keyword represents an upper bound for the type – T or any sub-type of T. You can also use super as in ? super T which provides a lower bound for the type – T or any super-type of T. Fall 2018 CISC124 - Prof. McLeod

15 Advanced Demo Since you cannot instantiate objects using a generic type, how would you write a generic factory method? GenericFactoryDemo.java Fall 2018 CISC124 - Prof. McLeod

16 Summary: Generic Factories
Factory methods generate objects, or more specifically, collections of objects. How can you make these methods non- type-specific? You cannot create an array of type T in a generic method unless you can tolerate an unchecked cast to T[]. You cannot instantiate objects of type T in a generic method directly. Fall 2018 CISC124 - Prof. McLeod

17 The Class<T> Object
You can pass a type into a method using a Class<T> object. The Object class contains a method .getClass() that returns a Class<T> object. Or you can just use the .class literal on a type. For example: String aString = "Hello!"; System.out.println(aString.getClass()); System.out.println(String.class); Shows: class java.lang.String Fall 2018 CISC124 - Prof. McLeod

18 The Class<T> Object, Cont.
See the API for lots of methods belonging to this Object. Useful for determining all kinds of information about a type or an instance. Provides a basis for the use of Reflection. Reflection is all about techniques for the remote discovery and use of class structures. You can even access private methods! module-info.java can control the use of Reflection. See: for example. Fall 2018 CISC124 - Prof. McLeod

19 From Before: Anonymous Class Example
public class AnonymousClassDemo { public static void main(String[] args) { MessageSender ms = new MessageSender() { public void sendGreeting (String name) { System.out.println("Hello " + name + "!"); } // end sendGreeting }; // end ms, NOTE ";"! ms.sendGreeting(“Steve"); } // end main } // end AnonymousClassDemo Fall 2018 CISC124 - Prof. McLeod

20 Anonymous Class Example - Cont.
MessageSender is an interface, not an Object: public interface MessageSender { void sendGreeting (String name); } // end MessageSender interface Java 8 now has a very tidy solution to using messy anonymous classes – Lambda Functions: Fall 2018 CISC124 - Prof. McLeod

21 The Lambda Version public class LambdaDemo { public static void main(String[] args) { MessageSender ms = name -> System.out.println("Hello " + name); ms.sendGreeting("Steve"); } // end main } // end LambdaDemo Fall 2018 CISC124 - Prof. McLeod

22 A Lambda Function Kind of like an “anonymous method”. Syntax:
Parameters for the method first. No parameters, use { }, one parameter by itself – no brackets required, more than one use (a, b, etc.). No types required. Then the fancy -> “arrow”. Then the method body. Put more than one statement inside { }. Can even define an inner interface: Fall 2018 CISC124 - Prof. McLeod

23 The Lambda Version, Cont.
public class LambdaDemoAgain { interface MessageSender { void sendGreeting(String aName); } // end MessageSender interface public static void main(String[] args) { MessageSender ms = name -> System.out.println("Hello " + name); ms.sendGreeting("Steve"); } // end main } // end LambdaDemoAgain Fall 2018 CISC124 - Prof. McLeod

24 Lambda Functions, Cont. Note how the abstract method in the interface determines the structure of the lambda function – the parameter and parameter type, the method name and the lack of a return statement. These functions could be useful! (Especially when attaching events to GUI components.) Only certain interface structures can be used. Fall 2018 CISC124 - Prof. McLeod


Download ppt "CISC124 Assignment 4 on Inheritance due today at 7pm."

Similar presentations


Ads by Google