Objects and Iterators

Similar documents
Introduction to Programming Using Java (98-388)

Java Primer 1: Types, Classes and Operators

Generic classes & the Java Collections Framework. *Really* Reusable Code

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Object Oriented Programming. What is this Object? Using the Object s Slots

CS 251 Intermediate Programming Inheritance

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

Java Programming Training for Experienced Programmers (5 Days)

CS260 Intro to Java & Android 03.Java Language Basics

Language Features. 1. The primitive types int, double, and boolean are part of the AP

CMSC 132: Object-Oriented Programming II

Generic Programming. *Really* reusable code

C++ (Non for C Programmer) (BT307) 40 Hours

Building Java Programs

Polymorphism. return a.doublevalue() + b.doublevalue();

This page intentionally left blank

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Inheritance. Transitivity

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

Subtype Polymorphism

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

Super-Classes and sub-classes

Chapter 4 Defining Classes I

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Method Overriding. Note that you can invoke the overridden method through the use of the keyword super.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Operators and Expressions

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

CORE JAVA TRAINING COURSE CONTENT

1 Shyam sir JAVA Notes

Inheritance and Interfaces

Absolute C++ Walter Savitch

Generic types. Announcements. Raw ArrayLists. Generic types (cont.) Creating a raw ArrayList: Accessing a raw ArrayList:

Closed book but one sheet, both sides, of A4 paper is allowed. Section 2.5 of the text Generics in the Java Programming Languages by Gilad Bracha

Index COPYRIGHTED MATERIAL

Implements vs. Extends When Defining a Class

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Programming II (CS300)

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages


MIT AITI Lecture 18 Collections - Part 1

Computer Science II (20082) Week 1: Review and Inheritance

15CS45 : OBJECT ORIENTED CONCEPTS

Example: Count of Points

Software Construction

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

C ONTENTS PART I FUNDAMENTALS OF PROGRAMMING 1. and Java 3. Chapter 1 Introduction to Computers, Programs,

CISC370: Inheritance

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

TeenCoder : Java Programming (ISBN )

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

(2½ hours) Total Marks: 75

Program Fundamentals

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

C a; C b; C e; int c;

CMSC 132: Object-Oriented Programming II

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Generics. IRS W-9 Form

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Binghamton University. CS-140 Fall Dynamic Types

CO Java SE 8: Fundamentals

CMSC132 Summer 2018 Midterm 1

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

VARIABLES AND TYPES CITS1001

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Abstract Classes and Interfaces

Java Review: Objects

CMSC132 Summer 2018 Midterm 1. Solution

Java SE 8 Programmer I and II Syballus( Paper codes : 1z0-808 & 1z0-809)

Type Hierarchy. Lecture 6: OOP, autumn 2003

5/23/2015. Core Java Syllabus. VikRam ShaRma

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

CLASS DESIGN. Objectives MODULE 4

Values and Variables 1 / 30

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

[TAP:AXETF] Inheritance

AP COMPUTER SCIENCE AB 2006 SCORING GUIDELINES

CompuScholar, Inc. 9th - 12th grades

CLASSES AND OBJECTS IN JAVA

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

A- Core Java Audience Prerequisites Approach Objectives 1. Introduction

APCS Semester #1 Final Exam Practice Problems

C++ Important Questions with Answers

Interfaces and related issues Reading for these lectures: Weiss, Section 4.4 (The interface), p no multiple inheritance with classes

Week. Lecture Topic day (including assignment/test) 1 st 1 st Introduction to Module 1 st. Practical

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

Conversions and Casting

CS 302: Introduction to Programming in Java. Lecture 15

Transcription:

Objects and Iterators Can We Have Data Structures With Generic Types? What s in a Bag? All our implementations of collections so far allowed for one data type for the entire collection To accommodate a different data type, we have to modify our implementation Can we have a generic collection that can hold any data type? Can we have a collection that can hold a mix of data types? 2 Widening Conversions Can we do this: public class Foo { } Foo x = new Foo(); Object y; y = x; This is called a widening conversion A widening conversion occurs when you assign an object to a more general (wider) reference variable 3

4 Narrowing Conversions Can we do this: public class Foo { } Foo x = new Foo(); Object y; y = x; Foo z; z = y; Narrowing Conversions No, but we can do this: z = (Foo) y; This is called a narrowing conversion A narrowing conversion occurs when you assign an object to a more specific (narrower) reference variable. We have to use a typecast in this case 5 Generic Collections Can we re-implement our collection classes (bags or sequences using arrays or linked lists) as generic collections that can hold any data type as their data? We can do this by having the data type be Object and using widening and narrowing conversions 6

7 Wrapper Classes With widening and narrowing conversions, we can implement generic collections (collections of Objects) for holding data of any type What about primitive data types? Wrapper classes are defined for primitive data types: Each wrapper class has an instance variable to hold the value Each wrapper class has a one-argument constructor to create a new object with a value Each wrapper class has an accessor method to retrieve the value of the object java allows autoboxing and autounboxing for the values of wrapper classes Standard Wrapper Classes Accessor Limitations of Using Wrappers Primitive operations (e.g. +, -) are no longer directly available Have to use equals for comparison rather than == All wrapper classes implement equals methods that test that their values are equal 9

10 Notes on Using Nodes of Objects Watch out for the use of equals ; overusing it can lead to incorrect code. Only use it when you are comparing data, not when you are trying to find out if 2 objects are really the same object Equals method cannot be used with a null object Notes on Using Nodes of Objects In most cases, when values are retrieved, they must be cast back to their original types When using a clone method, all objects must be cloned as well. This is called deep cloning 11 Generic Methods Generic methods are methods with generic parameters Generic parameter types must be specified in the header of the method preceding the return type Only object types (not basic types) can be substituted for generic types 12

13 Example Generic Method Generic Classes A generic class relies on a generic data type, such as: public class ArrayBag<E> { private E[ ] data; 14 Limitations Only object types can be substituted for the generic types new arrays of the generic type cannot be directly created: solution: create an array of Object and type cast it 15

16 JCL Java Collections Library Java implements several collections classes, such as: Vector and ArrayList: very similar to an array: see Appendix D of the book for implementation Set: similar but enforces that any object can be in the set only once See Java documentation for more collections JCL classes all use objects and can thus be used with any object type Collection Utility Methods binary search: Implemented as a static method of the class Arrays sort: Implemented as a static method of the class Arrays Both methods are overloaded with implementations for the eight primitive types and for Objects 17 Interfaces Interfaces are classes with methods that can be used by any class to simulate multiple inheritance Java provides several built-in interfaces such as: Cloneable (the clone method) Comparable (the compareto method) Users can design their own interfaces by using the keyword interface to declare the interface classes 18

19 Implementing Interfaces To implement an interface, a class must: declare that it implements the interface implement all the methods for the interface A class may implement more than one interface by declaring all of them (separated by commas) Sample interface Declaration 20 Sample Interface Implementation 21

22 Interfaces Whether or not a class implements a particular interface can be tested using the instanceof operator The instanceof operator can also be used to test if an object is an instance of a given class (or any of its super classes) Question to Think About? What is the advantage of using interfaces as opposed to simply implementing the same methods in the different classes? 23 Iterators A Java interface requiring the following methods: public boolean hasnext() public Object next() public void remove() Must import java.util.iterator to use 24

25 Types of Iterators Internal Iterators: Implement the iterator methods as part of the collection class itself External Iterators: Implement the iterator as a separate class with a method in the original class to create an iterator Has the added advantage of having multiple iterators for the same object simultaneously 26 27

28 29 30

31