






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Summer I 2008;
Typology: Study notes
1 / 30
This page cannot be seen from the preview
Don't miss anything!























Overview
Introduction to OOP principles
Review language constructs Introduce new language constructs Many from Java 5.
Techniques – Abstraction
Provide high-level model of activity or data
Specify what actions should be performed Hide algorithms
Specify data objects for problem Hide representation
Techniques – Encapsulation
Confine information so it is only visible / accessible through an associated external interface
For some entity X in program Abstract data in X Abstract actions on data in X Collect data & actions on X in same location Protects and hides X
Java Programming Language
Example Interface – specifies a contract Class – implements/defines contracts, supports encapsulation of implementation
Example Java Collections Framework Java Swing
Java Interface
Collection of Constants Abstract methods; no implementations Can not be instantiated
Must implement all methods in interface Example class Foo implements Bar { … }
But class can “inherit” from multiple interfaces
Overview
Introduction to OOP principles
Review language constructs Introduce new language constructs Many from Java 5.
Review of Java Language Constructs
Primitive types, variables, constants, operators If-else, switch, while, for
Object instances Creating objects with new Object references The null reference Instance data, class (static) data Methods Parameters, return values, polymorphism
New Java Language Constructs
Iterator interface
Enumerated Types New type of variable with set of fixed values Establishes all possible values by listing them Supports values(), valueOf(), name(), compareTo()… Can add fields and methods to enums Example public enum Color { Black, White } // new enumeration Color myC = Color.Black; for (Color c : Color.values()) System.out.println(c); When to use enums Natural enumerated types – days of week, phases of the moon, seasons Sets where you know all possible values
Generics – Motivating Example
Utility classes handle arguments as Objects Objects must be cast back to actual class Casting can only be checked at runtime
class A { … } class B { … } List myL = new List(); myL.add(new A()); // Add an object of type A … B b = (B) myL.get(0); // throws runtime exception // java.lang.ClassCastException
Solution – Generic Types
Provides abstraction over types Can parameterize classes, interfaces, methods Parameters defined using notation
public class foo { … } List myNames = ...
Readability & robustness
Generics – Issues
Even if class A extends class B List does not extend List
class B { … } class A extends B { … } // A is subtype of B B b = new A(); // A used in place of B List aL = new LinkedList(); List bL = aL; // compile time error
Subtyping and generic types
class B { … } class A extends B { … } // A is subtype of B B b = new A(); // A can be used where B expected List aL = new LinkedList(); List bL = aL; bL.add(b); A a = aL.getFirst(); // runtime exception Example (subtyping package)