CISC124 Assignment 4 on Inheritance due today at 7pm.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Generics In Java 1.5 By Manjunath Beeraladinni. Generics ➲ New feature in JDK1.5. ➲ Generic allow to abstract over types. ➲ Generics make the code clearer.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Chapter 9: Polymorphism Coming up: Creating Objects Revisited.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
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.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
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.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Today Enumerated Types - Summary. Start Nested Classes: Inner Classes, Local Classes, Anonymous Classes, Lambda Functions Next: Interfaces, Abstract Classes.
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.
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Generics, Lambdas, Reflections
Fall 2017 CISC124 9/18/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
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.
Generics.
Object Oriented Programming
Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…
Chapter 19 Generics Dr. Clincy - Lecture.
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.
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.
Generics.
Generic programming in Java
METHOD OVERRIDING in JAVA
CISC124 Assignment 4 on Inheritance due next Friday.
CISC/CMPE320 - Prof. McLeod
CISC124 Assignment 3 due tomorrow at 7pm.
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
CISC124 Labs start this week in JEFF 155. Fall 2018
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
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.
CMSC 202 Generics.
Generics, Lambdas, Reflections
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.
Winter 2019 CMPE212 4/20/2019 CMPE212 – Reminders
CMPE212 – Reminders Course Web Site:
Last Time Some discussion of program “efficiency”. Arrays – 1D and 2D
Chapter 11 Inheritance and Polymorphism Part 2
Review: libraries and packages
Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
CMPE212 – Reminders Assignment 2 due today, 7pm.
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
Chapter 19 Generics.
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Advanced Programming in Java
Generics, Lambdas and Reflection
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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 @SuppressWarnings("unchecked")) Use of Class<T> may be a way around this problem. Fall 2018 CISC124 - Prof. McLeod

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

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

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

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

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

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

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: http://tutorials.jenkov.com/java-reflection/index.html, for example. Fall 2018 CISC124 - Prof. McLeod

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

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

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

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

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

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