13-1 13 Generic abstraction  Genericity  Generic classes  Generic procedures Programming Languages 3 © 2012 David A Watt, University of Glasgow.

Slides:



Advertisements
Similar presentations
MT311 (Oct 2007) Java Application Development Object-Oriented Programming Languages Tutorial 8.
Advertisements

Modules Program is built out of components. Each component defines a set of logically related entities (strong internal coupling) A component has a public.
Java Programming Abstract classes and Interfaces.
Abstract Classes We often define classes as subclasses of other classes – First, define the superclass – In some cases, a superclass may not have enough.
1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet.
Interfaces.
Generic programming in Java
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
1 L39 Generics (1). 2 OBJECTIVES  To create generic methods that perform identical tasks on arguments of different types.  To create a generic Stack.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
OOP Languages: Java vs C++
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Course: Object Oriented Programming - Abstract Data Types Unit1: IntroductionSlide Number 1 Introduction Course: Object Oriented Programming Abstract Data.
6 Stack ADTs  Stack concepts  Stack applications  Stack ADTs: requirements, contracts  Implementations of stacks: using arrays and linked-lists  Stacks.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Effective Java: Generics Last Updated: Spring 2009.
Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10.
Porting Implementation of Packet Utilization Standard from ADA to JAVA Annelie Hultman (TEC-EME) Donata Pedrazzani (TEC-EMS) ESA/ESTEC 2004 JPUS de-briefing.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
CIS 270—Application Development II Chapter 18-Generics.
CS-2851 Dr. Mark L. Hornick 1 Generic Java Classes Implementing your own generic classes.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Coming up: Inheritance
Interfaces and Inner Classes
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
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.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
ISBN Chapter 12 Support for Object-Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 5:Interfaces and Abstract Classes
ADT’s, Collections/Generics and Iterators
Chapter 20 Generic Classes and Methods
Chapter 3: Using Methods, Classes, and Objects
A tree set Our SearchTree class is essentially a set.
12 Data abstraction Packages and encapsulation
Java Programming Language
A tree set Our SearchTree class is essentially a set.
Implementing Non-Static Features
Arrays and Collections
Chapter 13 Abstract Classes and Interfaces Part 01
Chapter 5 Classes.
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Generic abstraction  Genericity  Generic classes  Generic procedures Programming Languages 3 © 2012 David A Watt, University of Glasgow

13-2 Genericity  A program-unit is generic if it is parameterized with respect to a type on which it depends.  Many reusable program-units (e.g., stack, queue, list, and set ADTs/classes) are naturally generic.  Generic program-units include: –generic packages (not covered here) –generic classes –generic procedures.

13-3 Generic classes  In Java, a generic class GC is parameterized with respect to a type T on which it depends: class GC { … T … T … }  The generic class must be instantiated, by substituting a type argument A for the type parameter T: GC  This instantiation generates an ordinary class.

13-4 Example: Java generic class (1)  Consider a class that encapsulates lists with elements of type T. This class can be made generic with respect to type T.  Generic class declaration: class List { // A List object is a list of elements of type // T, where the number of elements ≤ cap. private static final cap = 100; private int size; private T[] elems; type parameter

13-5 Example: Java generic class (2)  Generic class declaration (continued): public List () { // Construct an empty list. size = 0; elems = (T[]) new Object[cap]; } public void add (T elem) { // Add elem to the end of this list. elems[size++] = elem; } }

13-6 Example: Java generic class (3)  The following instantiation generates a class that encapsulates lists with String elements: List  The generated class can be used like an ordinary class: List sentence; sentence = new List (); sentence.add("…"); type argument (substituted for type parameter T )

13-7 Example: Java generic class (4)  The following instantiation generates a class that encapsulates lists with Date elements: List holidays; holidays = new List (); holidays.add (new Date(2009, 1, 1));  In an instantiation, the type argument must be a class, not a primitive type: List primes; illegal

13-8 Java generic interface Comparable  Java also supports generic interfaces.  From java.lang : interface Comparable { public int compareTo (T that); }  If class C is declared as implementing Comparable, C must be equipped with a compareTo method that compares objects of type C.

13-9 Bounded type parameters (1)  Consider a generic class GC‹T› that requires T to be equipped with some specific methods.  T may be specified as bounded by a class C: class GC { … } –T is known to be equipped with all the methods of C. –The type argument must be a subclass of C.  Alternatively, T may be specified as bounded by an interface I: class GC { … } –T is known to be equipped with all the methods of I. –The type argument must be a class that implements I.

13-10 Bounded type parameters (2)  Recall a generic class GC‹T› that does not require T to be equipped with any specific methods. As we have seen, it is enough just to name T: class GC { … }  This is actually an abbreviation for: class GC { … } –T is known to be equipped with all the Object methods, such as equals. –The type argument must be a subclass of Object, i.e., any class.

13-11 Example: Java generic class with bounded type parameter (1)  Consider a class Pqueue that encapsulates priority queues with elements of type T. It is required that T is equipped with a compareTo method.  Generic class declaration: class PQueue > { private static final cap = 20; private int size; private T[] elems; public PQueue () { size = 0; elems = (T[]) new Object[cap]; }

13-12 Example: Java generic class with bounded type parameter (2)  Generic class declaration (continued): public void add (T elem) { // Add elem to this priority queue. if (elem.compareTo(elems[0])<0) … … } public T first () { // Return the first element of this priority queue. return elems[0]; } }

13-13 Example: Java generic class with bounded type parameter (3)  Class String implements Comparable. So the following generates a class that encapsulates priority queues with String elements: PQueue pq; pq = new PQueue (); pq.add("beta"); pq.add("alpha"); out.print(pq.first());

13-14 Example: Java generic class with bounded type parameter (4)  Suppose that class Date implements Comparable. Then the following generates a class that encapsulates priority queues with Date elements: PQueue holidays;  But PQueue cannot be instantiated with a class C that does not implement Comparable : PQueue buttons; illegal

13-15 Generic procedures  A Java method may be parameterized with respect to a type T on which the method depends.

13-16 Example: Java generic method  A method that chooses between two arguments of type T can be made generic w.r.t. type T : public static T either (boolean b, T y, T z) { return (b ? y : z); }  Calls: … either (isletter(c), c, '*') … either (m > n, m, n) implicitly subst- ituting type Character for T implicitly subst- ituting type Integer for T