CSC 243 – Java Programming, Spring 2014

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.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
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.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
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.
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.
IBM TSpaces Lab 2 Customizing tuples and fields. Summary Blocking commands Tuple Expiration Extending Tuples (The SubclassableTuple) Reading/writing user.
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,
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.
1 Chapter 21 Generics. 2 Objectives F To use generic classes and interfaces (§21.2). F To declare generic classes and interfaces (§21.3). F To understand.
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.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
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.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
External Scope CECS 277 Mimi Opkins.
Lecture 5:Interfaces and Abstract Classes
Java Generics.
More Sophisticated Behavior
Use interfaces to define constants?
Chapter 20 Generic Classes and Methods
Generics, Lambdas, Reflections
Interfaces and Inheritance
Generics.
Object Oriented Programming
Chapter 19 Generics Dr. Clincy - Lecture.
Object-Oriented Programming: Polymorphism
Generics (Parametric Polymorphism)
Java Programming Language
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
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.
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.
CSC 220 – Processing Spring, 2017
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.
Introduction to Classes and Objects
Presentation transcript:

CSC 243 – Java Programming, Spring 2014 March, 2014 Week 7ish, Generics

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 Client code supplies a binding for each generic parameter that must be an interface or class name. It cannot be a primitive type. A generic parameter may constrain the type of class or interface that client code can bind using a wildcard. ?, ? extends T, ? super T – details follow.

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. The writer of a generic class must ensure type consistency within the class. The user 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.