Typecasting References Computer Science 3 Gerb Reference: Objective: Understand how to use the Object class in Java in the context of ArrayLists.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Java Review Interface, Casting, Generics, Iterator.
Computer Science 209 Software Development Equality and Comparisons.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
1 Java Object Model Part 1. 2 Type Definition: set of values – a set of values and set of operations –a set of operations that can be applied to those.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Generics and Type Safety Recitation – 4/24/2009 CS 180 Department of Computer Science, Purdue University.
Exam Objective : Legal return types PRESENTED BY : SRINIVAS VG.
Alice in Action with Java
Topic 6 Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
ADSA: Subtypes/ Advanced Data Structures and Algorithms Objective –explain how subtyping/subclassing and generics are combined in Java –introduce.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures and Java CS 105. L7: Java Slide 2 Data structure Data structure defined: A systematic way of organizing and accessing data Examples Dictionary:
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 10 – Programming with Java Datatypes Type Casting.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Java Generics Compiled from Core Java Technologies Tech Tips By Billy B. L. Lim.
Generics. What is Generics Collections can store Objects of any Type Generics restricts the Objects to be put in a collection Generics ease identification.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
Generic Types  Recent release of Java added generics  Include type parameters in class definition  Like methods, parameters can change each time 
Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008.
Types in programming languages1 What are types, and why do we need them?
Introduction to Generics
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
CS-2851 Dr. Mark L. Hornick 1 Generic Java Classes Implementing your own generic classes.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Introduction to Java Primitive Types Operators Basic input and output.
Inheritance and Access Control CS 162 (Summer 2009)
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Object-Oriented Programming Part 3 Bank Account Embezzling Hood College JETT Workshop Dept. of Computer Science February Objectives of this presentation:
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones EE 422CGenerics 1.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
CSE 1201 Object Oriented Programming ArrayList 1.
Superclasses and Subclasses in Java Computer Science 3 Gerb Objective: Understand superclass methods and the “this” keyword in Java.
Introduction to Objects and Encapsulation Computer Science 4 Mr. Gerb Reference: Objective: Understand Encapsulation and abstract data types.
Question of the Day  Move one matchstick to produce a square.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Sixth Lecture ArrayList Abstract Class and Interface
Recitation 6 Inheritance.
Chapter 19 Generics Dr. Clincy - Lecture.
Chapter 11 Inheritance and Polymorphism Part 2
Creating and Modifying Text part 3
Subtype Substitution Principle
Java Coding 6 David Davenport Computer Eng. Dept.,
Presentation transcript:

Typecasting References Computer Science 3 Gerb Reference: Objective: Understand how to use the Object class in Java in the context of ArrayLists.

Extracting Objects from Arraylists Recall that the remove and get operations on ArrayLists return an Object You must cast into the class you expect it to be: String s= (String) myList.get(0); Why? Three rules apply to determining when casting is required for converting from one class to another.

Rule#1: Casting is required when converting an instance of a superclass (like Object) into one of its subclasses Could go wrong if the superclass instance isn’t an instance of the subclass. E.g. not all Objects are strings E.g. If casting a Flower to a Rose, what if the flower is not a Rose, but a Daisy? The cast tells java to check if it’s ok Rose myRose = (Rose) flower;

Rule#2: Casting is not required when converting instance of a subclass into one of its superclasses Could not go wrong. E.g. If casting a Rose to a Flower, well all Roses are Flowers Flower myFlower = myRose;

Rule#3: Casting is impossible from one object to another if neither is a subclass or superclass of the other. Impossible. Can’t use casting to turn a Rose into a Daisy Gives a compiler error when possible, and an exception all the time Rose myRose = myDaisy;

Therefore... Must keep track of what types you store: ArrayList myList = new ArrayList(); Integer myInt = new Integer(5); myList.add(myInt); Adds Integer wrapper myList.add(“A string”); Adds a string String s= (String) myList.get(0); No compiler error, since String is an object But will crash with a type conversion error, because get will return an Integer, which can’t be cast into a String.

Summary Java provides the ArrayList class to implement the List interface –Array implementation –Define an interface by listing its methods Things to keep in mind when using Array Lists –Stores objects. –Primitive types are not objects. –Must keep track of what type of object ArrayList operations will return or risk a type conversion error.