CS2_23_GenericClasses - CS2 Module 23 Category: OO Concepts...

Info iconThis preview shows pages 1–18. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS2 Module 23 Category: OO Concepts Topic: Generic Classes Objectives Generic Classes Using Polymorphism Wrapper Classes Abstract methods & classes Interfaces Comparable Constants CS 2 Introduction to Object Oriented Programming Module 23 OO COncepts Generic Classes G e n e r i c C l a s s e s All Container Classes you build should be Generic Generic classes are [ almost ] trivial to build Every class is derived from the class Object Make this the type of the contained data An object of any type can then be put into the container You have to cast the object on output to its specific class The almost : Primitive data types [int, char, double] dont work like this your first, trivial example programs need a little TLC each atomic data element has a corresponding wrapper class for this purpose e.g. the Integer class is an Object-type container for an int. Example Suppose we have a class StudentRecord which holds information about a student class StudentRecord { private String name; private int ssn; // etc. We want to put them in a linked list class StudentNode { private StudentRecord data; private StudentNode next; // etc. The List class StudentList { private StudentNode head; public void add(StudentRecord data) { // etc public StudentRecord removeFront() { // etc. Using it... // Assume we have some StudentRecords StudentList sl = new StudentList(); sl.add(sr1); sl.add(sr2); sl.add(sr3); StudentRecord srTemp; srTemp = sl.removeFront(); System.out.println(srTemp.getName()); Now...everytime we want to make a list of anything we need to modify all of our code This is a "bad thing" Solution Recall that all classes are descendants of class Object. Thus our simple class StudentRecord is really an Object class StudentRecord { private String name; private int ssn; // etc. So we modify our list node class ListNode { private Object data; private ListNode next; // etc. And now produce a "generic" linked list class LinkedList { private ListNode head; public void add( Object data) { // etc public Object removeFront() { // etc. Using it... // Assume we have some StudentRecords LinkedList ll = new LinkedList(); ll.add(sr1); ll.add(sr2); ll.add(sr3); StudentRecord srTemp; srTemp = (StudentRecord) (ll.removeFront()); System.out.println(srTemp.getName()); Now, since everything is an Object our LinkedList can hold anything Everything is not an Object! What about primitives? Wrapper Classes That's why we have wrapper classes! Each wrapper class can create an object that will hold a primitive Primitive Wrapper boolean Boolean char Character byte Byte short Short int Integer long Long float Float double Double Primitive Wrapper boolean Boolean char Character byte Byte short Short int Integer long Long float Float double Double We can "wrap" the primitive in a wrapper and put that object in our generic class Questions?Questions?...
View Full Document

This note was uploaded on 01/23/2012 for the course CS 1371 taught by Professor Stallworth during the Fall '08 term at Georgia Institute of Technology.

Page1 / 62

CS2_23_GenericClasses - CS2 Module 23 Category: OO Concepts...

This preview shows document pages 1 - 18. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online