1 / 9

Ch. 2 Object-Oriented Programming

Ch. 2 Object-Oriented Programming. Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in Java, Goodrich, Tamassia and Goldwasser ( Wiley 2016). Advanced Java Generics. Type inference.

lopezd
Download Presentation

Ch. 2 Object-Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ch. 2Object-Oriented Programming Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in Java, Goodrich, Tamassia and Goldwasser (Wiley 2016)

  2. Advanced Java Generics

  3. Type inference • So far we learned to instantiate our generic objects like this: • List<Integer> l = new List<Integer>(); • However the Java compiler (with more recent versions) can infer the type parameters based on the first declaration • List<Integer> l = new List<>(); // Note that the <> are specified!

  4. Bounded Type parameters • We can specify constraints on the generic parameters through interfaces to only accept certain types in generic classes/functions • This extends the functionality of generic data to the methods of that interface public <T extends Comparable<T>> intcompareTwo(T a, T b) { returna.compareTo(b); // Comparable<T> enforces a // having the compareTo function } • extends is always used, it generally refers to extends or implements in this context • You can have multiple bounds as well:public class Foo<T extends A & B & C>

  5. Type Erasure • As we have seen so far, type erasure means that our generic data was actually just java.lang.Object all along (with automatic casting) • However, when we use bounded type parameters, we inform Java not to treat them as just Object, but as the bounded type.

  6. Inheritance with Generics • Thought question: Say class Fooextendsclass Bar • Is a List<Foo> a subtype of List<Bar>? • Actually no! It is important to know that inheritance is not applied to the type parameters, as in:A List<Foo> is not also a List<Bar> • Still, as we have seen generic objects can subtype other generic objects, e.g., LeakyStack, ArrayList, etc.

  7. Wildcards • However, we can solve the subtype issue with wildcards • In Java, in generic code a '?' is called a wildcard. It represents an unknown type, so List<Foo> and List<Bar> are subtypes of List<?> (but not List<Object>) • Can be used to iterate over a generic list like so:public static void print(List<?> list) {for(Object e : list) System.out.print(e + " ");System.out.println();} • Note that print can be used with a list of any type of object

  8. Wildcards • You can additionally place bounds on wildcards using extends (for upper bounds) and super (for lower bounds) • So we will see this:public class Foo<T extends Comparable<? super T>>meaning Foo accepts any type that is comparable. Note that the comparable interface can be supported by any superclass of T

  9. Exercise • With your team. Create a generic interface called Addable with one function add • Create two classes that implement Addable: MyString and Point. • MyString owns a single string and uses concatenation for adding • Point should be a 2D point in the Cartesian plane. Adding will be the sum of components. • Create a generic function sum that sums an array of Addable objects • Test your function in mainwith both random MyStrings and Points

More Related