COMP 249: Object Oriented Programming II. Tutorial 11: Generics

Similar documents
COMP 249: Object Oriented Programming II. Tutorial 2: Intro to Inheritance

Chapter 6: Inheritance

CSC 172 Data Structures and Algorithms. Lecture 3 Spring 2018 TuTh 3:25 pm 4:40 pm

Implementing non-static features

For this section, we will implement a class with only non-static features, that represents a rectangle

ITI Introduction to Computing II

ITI Introduction to Computing II

What are Generics? e.g. Generics, Generic Programming, Generic Types, Generic Methods

Objects and Classes (1)

Reviewing OO Concepts

This exam is open book. Each question is worth 3 points.

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

Classes as Blueprints: How to Define New Types of Objects

First Name: AITI 2004: Exam 2 July 19, 2004

Solutions to Quiz 1 (March 14, 2016)

Slide 1 CS 170 Java Programming 1

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Quiz 1 (March 14, 2016)

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

The Nervous Shapes Example

AP COMPUTER SCIENCE A

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

About This Lecture. Data Abstraction - Interfaces and Implementations. Outline. Object Concepts. Object Class, Protocol and State.

First Name: AITI 2004: Exam 2 July 19, 2004

Java Comparable interface

CSC 102 Lecture Notes Week 2 Introduction to Incremental Development and Systematic Testing More Jav a Basics

CMP-326 Exam 2 Spring 2018 Total 120 Points Version 1

S.O.L.I.D: Software Engineering Principles

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Chapter 21- Using Generics Case Study: Geometric Bunch. Class: Driver. package csu.matos; import java.util.arraylist; public class Driver {

Topic 7: Algebraic Data Types

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

8359 Object-oriented Programming with Java, Part 2. Stephen Pipes IBM Hursley Park Labs, United Kingdom

Chapter 4 Defining Classes I

Inheritance and Polymorphism

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

3.1 Class Declaration

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

Introduction to Programming Using Java (98-388)

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Outline. Object-Oriented Design Principles. Object-Oriented Design Goals. What a Subclass Inherits from Its Superclass?

CS1004: Intro to CS in Java, Spring 2005

CISC370: Inheritance

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

Java interface Lecture 15

Inheritance, Polymorphism, and Interfaces

Reviewing OO Concepts

Java Generics -- an introduction. Based on

CS1083 Week 2: Arrays, ArrayList

Chapter 6 Class and Method

Distributed Systems Recitation 1. Tamim Jabban

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Keyword this. Can be used by any object to refer to itself in any class method Typically used to

CS 101 Exam 1 Spring 200 Id Name

EECS168 Exam 3 Review

Programming Exercise 14: Inheritance and Polymorphism

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

Defining Classes and Methods

Chapter 6 Lab Classes and Objects

OOP, null, Graphics. Brooks Townsend

Computer Science 210: Data Structures

Introductory Programming Inheritance, sections

ITI Introduction to Computing II

Java Primer. CITS2200 Data Structures and Algorithms. Topic 0

Solution to Test of Computer Science 203x Level Score: / 100 Time: 100 Minutes

8. Polymorphism and Inheritance

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

CS 101 Fall 2006 Midterm 3 Name: ID:

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

Programming Exercise 7: Static Methods

Distributed Systems Recitation 1. Tamim Jabban

2. [20] Suppose we start declaring a Rectangle class as follows:

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

CSCI 1301: Introduction to Computing and Programming Spring 2019 Lab 10 Classes and Methods

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

CS-202 Introduction to Object Oriented Programming

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Encapsulation in Java

Chapter 4: Writing Classes

Chapter 6 Introduction to Defining Classes

Making New instances of Classes

Generics, Type Safety, and Dynamic Data Structures

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Section: Inheritance Heck

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ June Exam

Aggregation. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

2. (True/False) All methods in an interface must be declared public.

Why Use Generics? Generic types Generic methods Bounded type parameters Generics, Inheritance, and Subtypes Wildcard types

Conception Orientée Objets. Programmation SOLID

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Principles of Software Construction: Objects, Design, and Concurrency. Objects (continued) toad. Spring J Aldrich and W Scherlis

Java Object Oriented Design. CSC207 Fall 2014

Final Exam May 21, 2003

Transcription:

COMP 249: Object Oriented Programming II Tutorial 11: Generics

What are generics and why use them? Generics enable types (Classes and interfaces) to be parameters when defining classes, interfaces and methods. Some advantages of using generics: Stronger type check at compile time. Fixing compiletime errors is easier than fixing runtime errors, which can difficult to find. Elimination of casts. Enabling programmers to implement generic algorithms.

A not generic Box class Consider the following Box class which is not generic: public class Box { private Object object; public void set(object object) { this.object = object; public Object get() { return object; The methods accept or return an Object. We cannot verify at compile time what specific Object is used. At some point this Object might be an int, later in the execution of the program, this same Object could be a String. This could result in runtime errors which are hard to debug.

Question 1: A simple generic class Write the same Box class as a generic. Your generic class should: Store a value of type T as a private variable. Implement accessor and mutator methods for that variable. Implement a tostring() method describing the box and its content. You should be able to use this class like this: Box<Integer> myintegerbox = new Box<Integer>(); myintegerbox.set(new Integer(10)); System.out.println(myIntegerBox); Box<String> mystringbox = new Box<String>(); mystringbox.set(new String("For sale, baby shoes, never worn")); System.out.println(myStringBox);

Question 2: Multiple Types A generic class can also take multiple Types. Let's build an OrderedPair generic class. Our generic class should: Store values of type T and S in private variables. Implement the accessor and mutators for each values. Implement the tostring() method describing the pair and their values. You should be able to use this class like this: OrderedPair<Integer,String> mypair = new OrderedPair<Integer,String>(); mypair.setfirst(new Integer(1)); mypair.setsecond(new String("So much depends on a red wheelbarrow")); System.out.println(myPair);

Question 3: Bounded Type Parameters At times, you might want to limit the types that can be used by a generic class. This can be accomplished with bounded type parameters using the extends word in the type definition. Let's create a NumberBox class that limits what our Box can contain to a Number class object. We should be able to use the first part of the following code, but generate an error on the second: // This works: NumberBox<Integer> myintegerbox = new NumberBox<Integer>(); myintegerbox.set(new Integer(10)); System.out.println(myIntegerBox); // This will result in a compile time error: NumberBox<String> mystringbox = new NumberBox<String>(); mystringbox.set(new String("I am not a Number!")); System.out.println(myStringBox);

Bounded Type Parameters with Multiple Bounds Bounded type parameters can also have multiple bounds. For example, you could ensure that your type is of a certain class and has access to certain interfaces: public class GenericClass<T extends Shape & Interface1 & Interface2> { //... That way, you are insuring that the variables and methods provided by Shape, Interface1 and Interface2 will be available to your GenericClass.

Question 4: Bounded Type Parameters with Multiple Bounds Consider the following Shape class and interface: abstract class Shape { private int height,width; public Shape() { this.height = 0; this.width = 0; public Shape(int h, int w) { this.height = h; this.width = w; public void setheigth(int i) { this.height = i; public void setwidth(int i) { this.width = i; public interface ShapeInterface { String printshape(); First, write a Rectangle class which extends Shape and implements ShapeInterface. The printshape() method should return a String of height by width characters. Now, create a generic which: public int getheigth() { return this.height; public int getwidth() { return this.width; is bounded to the Shape class and the ShapeInterface interface. works just like our Box generic, with accessors and mutators. returns the String generated by printshape() in the tostring() method.

References The Java Tutorial on Generics docs.oracle.com/javase/tutorial/java/generics/index.html Another Tutorial on the same subject, using diferent examples docs.oracle.com/javase/tutorial/extra/generics/index.html