CSC 220 – Processing Spring, 2017

Slides:



Advertisements
Similar presentations
CSC 243 – Java Programming, Spring 2013 March 26, 2013 Week 8, java.lang.Clonable & java.io.Serializable Interfaces.
Advertisements

ITEC200 – Week03 Inheritance and Class Hierarchies.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
1 Chapter 21 Generics. 2 Objectives F To know the benefits of generics (§21.1). F To use generic classes and interfaces (§21.2). F To declare generic.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Java Implementation: Part 3 Software Construction Lecture 8.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
CSC 243 – Java Programming, Spring 2013 March 12, 2013 Week 7, Generics.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
SPL/2010 Synchronization 1. SPL/2010 Overview ● synchronization mechanisms in modern RTEs ● concurrency issues ● places where synchronization is needed.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
CSC 520 – Advanced Object Oriented Programming, Fall, 2010 Thursday, September 30 Week 5, Generics and Inheritance Techniques, Meyer Ch. 10 & 16.
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.
The AP Java Subset Topics. A Topics Primitive Types int double boolean.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Java Generics.
More Sophisticated Behavior
CSC 243 – Java Programming, Spring 2014
Objects as a programming concept
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
Chapter 20 Generic Classes and Methods
Generics, Lambdas, Reflections
Interfaces and Inheritance
Generics.
Object Oriented Programming
Chapter 9 Inheritance and Polymorphism
Chapter 19 Generics Dr. Clincy - Lecture.
Extending Classes.
Generics (Parametric Polymorphism)
Java Programming Language
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
The Building Blocks Classes: Java class library, over 1,800 classes:
CSE 1030: Data Structure Mark Shtern.
Generics.
Building Java Programs
Generic programming in Java
Chapter 19 Generics.
Chapter 21 Generics.
Comp 212: Intermediate Programming Lecture 18 – Java Generics
CMSC 202 Generics.
Generics, Lambdas, Reflections
Chapter 19 Generics Jung Soo (Sue) Lim Cal State LA.
Java Programming Language
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Final and Abstract Classes
Chapter 21 Generics.
Chapter 19 Generics.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Chapter 19 Generics.
Presentation transcript:

CSC 220 – Processing Spring, 2017 March / April, 2017 Java Generics in Processing

Generic Interfaces / Classes We have been using generic interfaces and classes from the library. java.util.Set<E>, java.util.HashSet<E> java.util.Map<K,V>, java.util.HashMap<K,V> They allow containers to contain different types (classes) of objects. Generic parameters such as E (element type), K (key type) and V (value type) above are types bound by client code at compile time.

Using Generic Classes Application code supplies a binding for each generic parameter that must be an interface or class name. It cannot be a primitive type such as int or float. Java Generic classes are comparable to Template classes in C++. Their main use is to allow a container class such as java.util.HashSet<E> to work with any Java class substituted by your code for class variable E.

Building Generics A class or interface specifies its generic type. HashSet<E>, HashMap<K, V>, etc. ? extends T is a bounded wildcard, T or a subclass ? super T is a lower-bounded wildcard, T or a superclass ? is an unbounded wildcard, same as ? Extends Object The bound constrains client code type binding. Struck-through parts not in CSC220 final. The writer of a generic class must ensure type consistency within the class. The application programmer can safely assume type compatibility if the compiler does not flag an error.

Erasure and Restrictions Unlike C++ template classes, the compiler generates only 1 copy of the class code for a generic class, erasing the generic type T information at run time. Every generic type T is treated as a java.lang.Object at run time. Writers of generic classes may need to cast to generic type T, E, K, V etc. in places. Writers of generic classes must ensure that those casts are valid. The compiler cannot ensure this.

Restrictions 1. No constructor – no “new T()” for generic T. 2. No “new T[]” – you must cast an “Object []” to a “T []” within the generic class code. This cast often fails at run time with a wildcard T. Make sure to test if you use arrays of wildcard generic types. 3. No generic T in a static field, static method or static initialization block of code. 4. Exception classes cannot be generic.

Non-template class topics java.lang.Cloneable Marker interface that says (to humans and tools), “Objects of this interface provide a public clone() method. See java.lang.Object.clone() clone() returns a new copy of its object. Its implementation often uses a private constructor to build the new object. An immutable object can return a reference to itself. It is like a C++ copy constructor. Do not clone 2D arrays. An array clone() clones only the first dimension.

Non-template class topics java.util.Comparable<T> Specifies method int compareTo(T o). Returns -1, 0 or 1 if object is logically <, == or > o. May require redefining equals() and hashCode() in class java.lang.Object. Just exclusive-OR (^) the hashCodes of the fields. java.util.Serializable Useful for storing objects in binary data files or sending them across a network. All fields in a Serializable object should be Serializable, primitive types or transient.