Chapter 19 Generics Dr. Clincy - Lecture.

Slides:



Advertisements
Similar presentations
CHAPTER 12 GENERICS Introduction to java 1. Assignment 5 Solution See Eclipse.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
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.
1 L40 Generics (2). 2 OBJECTIVES  To understand raw types and how they help achieve backwards compatibility.  To use wildcards when precise type information.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Effective Java: Generics Last Updated: Spring 2009.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Generics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
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.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
 Pearson Education, Inc. All rights reserved. 1 Ch 18 Generics OBJECTIVES In this chapter you will learn:  To create generic methods that perform.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Lecture 5:Interfaces and Abstract Classes
Java Generics.
Chapter 15 Abstract Classes and Interfaces
Abstract Classes and Interfaces in Java Reference: COS240 Syllabus
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
CIS265/506 Cleveland State University – Prof. Victor Matos
More on Java Generics Multiple Generic Types Bounded Generic Types
John Hurley Cal State LA
Generic(Parameterized ) Types
Chapter 20 Generic Classes and Methods
Chapter 13 Abstract Classes and Interfaces
Generics, Lambdas, Reflections
Continuing Chapter 11 Inheritance and Polymorphism
Chapter 13 Abstract Classes and Interfaces
Object Oriented Programming
Chapter 9 Inheritance and Polymorphism
Generics (Parametric Polymorphism)
Generics.
Chapter 12 Abstract Classes and Interfaces
Generic programming in Java
Chapter 19 Generics.
Chapter 21 Generics.
Java – Inheritance.
Java Inheritance.
Generics, Lambdas, Reflections
CS2013 Lecture 3 John Hurley Cal State LA.
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
Chapter 19 Generics Jung Soo (Sue) Lim Cal State LA.
CSC 220 – Processing Spring, 2017
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 2
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 13 Abstract Classes and Interfaces Part 01
Chapter 21 Generics.
Chapter 19 Generics.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Chapter 19 Generics.
Presentation transcript:

Chapter 19 Generics Dr. Clincy - Lecture

Example – How is it implemented ? public class GeometricObject { . Subclass Superclass public class Circle extends GeometricObject { . public class Rectangle extends GeometricObject { . The keyword extends tells the compiler that the subclass extends the superclass, thus, the subclass inherits the superclass’ methods Dr. Clincy ------ Lecture

Abstract Classes and Abstract Methods Since area and perimeter can be computed for all geometric objects, its better to define the getArea() and getPerimeter() methods in the SuperClass, GeometricObject – designated using the abstract modifier in the class header However, these methods cannot be implemented in the SuperClass, GeometricObject, because their implementation depends on the specific type of geometric object – these are called abstract methods – designated using the abstract modifier in the method header Dr. Clincy - Lecture 3

Motivations SuperClass Classes become more specific and concrete with each new subclass Moving from a subclass back up to a superclass, the classes become more general and less specific SubClass1 SubClass2 SubClassN Sometimes, a superclass is so general, it cannot be used to create any specific instance – such a class is referred to as an abstract class Dr. Clincy - Lecture 4

What is an Interface ? An interface is a classlike construct that contains only constants and abstract methods. In many ways, an interface is similar to an abstract class, but the intent of an interface is to specify common behavior for objects. For example, given the classes fish, orange, apple and soup, we could use the interface edible to describe some common behavior or use. Instead of reinventing the wheel, we can use existing classes and methods to specify common behavior for objects Dr. Clincy - Lecture 5

The ArrayList Class – original “generic” class You can create an array to store objects. But the array’s size is fixed once the array is created. Java provides the ArrayList class that can be used to store an unlimited number of objects. Various methods for the ArrayList class Dr. Clincy ------ Lecture

The Issue Let’s say we want a class and associated methods to be used for multiple object types – write the code once If you write the class so that any object type can be used, the problem is, we will have to cast our objects back to class we want, in order to use it (too much casting is not good code – prone to runtime errors – the worst errors)\ The above was the solution prior to JDK 1.5 (or Java 5) The benefit and intent of the ArrayList class was that it can hold any object type The benefit of the ArrayList was, we can determine how the ArrayList class will work without having to specify what objtect types the ArrayList holds Once we instantiate an instance of the ArrayList class, we would tell it the object type our ArrayList will work with – in this case, no other object type is allowed Recall - With this approach, we had to cast it back to the class we needed If it is incorrectly casted, you will get a runtime error To fix this problem, we need to be able to tell Java what our ArrayList object is at compile time (versus it appearing at runtime). After JDK 1.5, this capability is now provided No casting needed Dr. Clincy - Lecture

Generic Type Old Way (prior to JDK 1.5) ArrayList cities = new ArrayList (); ArrayList is known as a generic class with a generic type E. You can specify a concrete type to replace E when creating an ArrayList. For example, the following statement creates an ArrayList and assigns its reference to variable cities. This ArrayList object can be used to store strings. ArrayList<String> cities = new ArrayList<String>(); Either syntax will work ArrayList<String> cities = new ArrayList<>(); Dr. Clincy ------ Lecture

Why Generics? The key benefit of generics is to enable errors to be detected at compile time rather than at runtime. A generic class or method permits you to specify allowable types of objects that the class or method may work with. If you attempt to use the class or method with an incompatible object, a compile error occurs. Dr. Clincy - Lecture

What is Generics? Why Generics? Generics is the capability to parameterize types. With this capability, you can define a class or a method with generic types that can be substituted using concrete types by the compiler. Why Generics? The key benefit of generics is to enable errors to be detected at compile time rather than at runtime. A generic class or method permits you to specify allowable types of objects that the class or method may work with. If you attempt to use the class or method with an incompatible object, a compile error occurs. Dr. Clincy - Lecture

Generic Instantiation Generic Type Generic Instantiation Runtime error Improves reliability Compile error Dr. Clincy - Lecture

Generic ArrayList in JDK 1.5 Dr. Clincy - Lecture

Declaring Generic Classes Generic type E declared < > used getSize peek push pop isEmpty Following example creates a stack to hold strings and adds 3 strings to the stack GenericStack<String> stack1 = new GenericStack<>(); stack1.push(“CS1302”); stack1.push(“CS3503”); Stack1.push(“CS4622”); Dr. Clincy - Lecture

Raw Type and Backward Compatibility A generic class used WITHOUT specifying a concrete type (called a raw type) enables backward compatibility with earlier versions of Java. // raw type ArrayList list = new ArrayList(); This is roughly equivalent to ArrayList<Object> list = new ArrayList<Object>(); Dr. Clincy - Lecture

Wildcards To specify a range for a generic type, unbounded wildcards, bounded wildcards or lower bound wildcards can be used ? unbounded wildcard ? extends T bounded wildcard ? super T lower bound wildcard Same as ? Extends Object represents T or a subtype of T denotes T or a supertype of T – recall the supertype comes from the superclass Dr. Clincy - Lecture

Erasure and Restrictions on Generics Generics are implemented using an approach called type erasure. The compiler uses the generic type information to compile the code, but erases it afterwards. So the generic information is not available at run time. This approach enables the generic code to be backward-compatible with the legacy code that uses raw types. Dr. Clincy - Lecture

Restrictions on Generics Restriction 1: Cannot Create an Instance of a Generic Type. Restriction 2: Generic Array Creation is Not Allowed. Restriction 3: A Generic Type Parameter of a Class Is Not Allowed in a Static Context. Restriction 4: Exception Classes Cannot be Generic. Dr. Clincy - Lecture