Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4, 16.4-16.5.

Slides:



Advertisements
Similar presentations
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Advertisements

Java Review Interface, Casting, Generics, Iterator.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Building Java Programs Chapter 16 Linked Lists. 2 A swap method? Does the following swap method work? Why or why not? public static void main(String[]
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 slides created by Marty Stepp
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Building Java Programs Interfaces, Comparable reading: , 16.4, 10.2.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Polymorphism & Interfaces
Building Java Programs Chapter 15 Implementing a Collection Class: ArrayIntList Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Computer Science 209 Software Development Java Collections.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh) reading: 9.5, 11.1, slides created by Marty Stepp
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
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.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
slides adapted from Marty Stepp and Hélène Martin
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Building Java Programs Interfaces reading: , 16.4.
Building Java Programs Generics, hashing reading: 18.1.
Lecture 5:Interfaces and Abstract Classes
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2
Sixth Lecture ArrayList Abstract Class and Interface
Building Java Programs Chapter 16
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets
Building Java Programs
Building Java Programs Chapter 16
A tree set Our SearchTree class is essentially a set.
Interfaces Professor Evan Korth.
Linked node problem 3 What set of statements turns this picture:
Generics (Parametric Polymorphism)
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh)
Building Java Programs
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
A tree set Our SearchTree class is essentially a set.
Binary Search Trees continued; Tree Sets
slides created by Marty Stepp
CSE 143 Lecture 25 Set ADT implementation; hashing read 11.2
Building Java Programs Chapter 16
CSE 373: Data Structures and Algorithms
Based on slides by Alyssa Harding & Marty Stepp
CSE 143 Lecture 25 Advanced collection classes
Hashing based on slides by Marty Stepp
Building Java Programs
slides created by Marty Stepp
Lecture 7: Linked List Basics reading: 16.2
CSE 143 Lecture 21 Advanced List Implementation
slides created by Marty Stepp
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,

2

3 A tree set Our SearchTree class is essentially a set. operations: add, remove, contains, size, isEmpty similar to the TreeSet class in java.util Let's actually turn it into a full set implementation. step 1: create ADT interface; implement it step 2: get rid of separate node class file step 3: make tree capable of storing any type of data (not just int ) We won't rebalance the tree, take a data structures class to learn how! ym pe k gc overallRoot

4 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations that can be performed on it. Describes what a collection does, not how it does it. Java's collection framework describes ADTs with interfaces: Collection, Deque, List, Map, Queue, Set, SortedMap An ADT can be implemented in multiple ways by classes: ArrayList and LinkedList implement List HashSet and TreeSet implement Set LinkedList, ArrayDeque, etc.implement Queue

5 Inner classes To get rid of our separate node file, we use an inner class. inner class: A class defined inside of another class. inner classes are hidden from other classes (encapsulated) inner objects can access/modify the fields of the outer object

6 Inner class syntax // outer (enclosing) class public class name {... // inner (nested) class private class name {... } Only this file can see the inner class or make objects of it. Each inner object is associated with the outer object that created it, so it can access/modify that outer object's methods/fields. If necessary, can refer to outer object as OuterClassName.this

7 Recall: Type Parameters ArrayList name = new ArrayList (); When constructing a java.util.ArrayList, you specify the type of elements it will contain in. ArrayList accepts a type parameter; it is a generic class. ArrayList names = new ArrayList (); names.add("Marty Stepp"); names.add("Helene Martin"); names.add(42); // compiler error

8 Implementing generics // a parameterized (generic) class public class name {... } Forces any client that constructs your object to supply a type. Don't write an actual type such as String; the client does that. Instead, write a type variable name such as E (for "element") or T (for "type"). You can require multiple type parameters separated by commas. The rest of your class's code can refer to that type by name.

9 Generics and inner classes public class Foo { private class Inner {...} // incorrect private class Inner {...} // correct } If an outer class declares a type parameter, inner classes can also use that type parameter. The inner class should NOT redeclare the type parameter. (If you do, it will create a second type param with the same name.)

10 Issues with generic objects public class TreeSet {... public void example(E value1, E value2) { // BAD: value1 == value2 (they are objects) // GOOD: value1.equals(value2) // BAD: value1 < value2 // GOOD: value1.compareTo(value2) < 0 } When testing objects of type E for equality, must use equals When testing objects of type E for, must use compareTo Problem: By default, compareTo doesn't compile! What's wrong!

11 Type constraints // a parameterized (generic) class public class name {... } A type constraint forces the client to supply a type that is a subclass of a given superclass or implements a given interface. Then the rest of your code can assume that the type has all of the methods in that superclass / interface and can call them.

12 Generic set interface // Represents a list of values. public interface Set { public void add(E value); public boolean isEmpty(); public boolean contains(E value); public void remove(E value); public int size(); } public class TreeSet > implements Set {...

13 Our list classes We have implemented the following two list collection classes: ArrayIntList LinkedIntList Problems: We should be able to treat them the same way in client code. Linked list carries around a clunky extra node class. They can store only int elements, not any type of value. Some methods are implemented the same way (redundancy). It is inefficient to get or remove each element of a linked list. index012 value front datanext 42 datanext -3 datanext 17

14 Generics and arrays (15.4) public class Foo { private T myField; // ok public void method1(T param) { myField = new T(); // error T[] a = new T[10]; // error myField = param; // ok T[] a2 = (T[]) (new Object[10]); // ok } You cannot create objects or arrays of a parameterized type. You can create variables of that type, accept them as parameters, return them, or create arrays by casting from Object[].

15 Common code Notice that some of the methods are implemented the same way in both the array and linked list classes. add( value ) contains isEmpty Should we change our interface to a class? Why / why not? How can we capture this common behavior?

16 Abstract classes (9.6) abstract class: A hybrid between an interface and a class. defines a superclass type that can contain method declarations (like an interface) and/or method bodies (like a class) like interfaces, abstract classes that cannot be instantiated (cannot use new to create any objects of their type) What goes in an abstract class? implementation of common state and behavior that will be inherited by subclasses (parent class role) declare generic behaviors that subclasses must implement (interface role)

17 Abstract class syntax // declaring an abstract class public abstract class name {... // declaring an abstract method // (any subclass must implement it) public abstract type name ( parameters ); } A class can be abstract even if it has no abstract methods You can create variables (but not objects) of the abstract type Exercise: Introduce an abstract class into the list hierarchy.

18 Abstract and interfaces Normal classes that claim to implement an interface must implement all methods of that interface: public class Empty implements IntList {} // error Abstract classes can claim to implement an interface without writing its methods; subclasses must implement the methods. public abstract class Empty implements IntList {} // ok public class Child extends Empty {} // error

19 An abstract list class // Superclass with common code for a list of integers. public abstract class AbstractIntList implements IntList { public void add(int value) { add(size(), value); } public boolean contains(int value) { return indexOf(value) >= 0; } public boolean isEmpty() { return size() == 0; } public class ArrayIntList extends AbstractIntList {... public class LinkedIntList extends AbstractIntList {...

20 Abstract class vs. interface Why do both interfaces and abstract classes exist in Java? An abstract class can do everything an interface can do and more. So why would someone ever use an interface? Answer: Java has single inheritance. can extend only one superclass can implement many interfaces Having interfaces allows a class to be part of a hierarchy (polymorphism) without using up its inheritance relationship.

21 Our list classes We have implemented the following two list collection classes: ArrayIntList LinkedIntList Problems: We should be able to treat them the same way in client code. Linked list carries around a clunky extra node class. They can store only int elements, not any type of value. Some of their methods are implemented the same way (redundancy). It is inefficient to get or remove elements of a linked list. index012 value front datanext 42 datanext -3 datanext 17