Class Notes (1,200,000)
US (500,000)
Cornell (1,000)
CS (100)
CS 2110 (30)
Gries (30)
Lecture 6

CS 2110 Lecture 6: Types and Their consequences, instanceof


Department
Computer Science
Course Code
CS 2110
Professor
Gries
Lecture
6

This preview shows page 1. to view the full 4 pages of the document.
Lecture 6 - Types and their consequences,
instanceof
Recall inside out rule
Given a method/class/anything inside some other
class/method/anything, the outer thing has access to any component
inside it
Generics
Values of linked lists are probably of class object
Public class LinkedList {
Void add(Object elem) {...}
Object get(int index) {...}
}
public class LinkedList<E> {
Void add(E elem) {...}
E get(int index) {...}
}
What does the <E> mean?
You put in Integer or String or
whatever type can be added to the list!
Today, we work with Class Animal
And subclasses Cat and Dog
Class hierarchy:
Object
Animal
Dog
Cat
Arrays!
Animal[] v = new Animal[3];
This creates an array with 3
elements, each whose type must be Animal
The array is also of type Animal[]
Assign new things to elements
v[0] = new Animal(...);
Stores a new object type of Animal at index 0 in
array v
find more resources at oneclass.com
find more resources at oneclass.com
You're Reading a Preview

Unlock to view full version