




























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; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;
Typology: Study notes
1 / 36
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 / accessiblethrough 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 – supports procedure abstraction Class – supports encapsulation
Example Java Collections Framework Java Swing
Java Interface
Collection of Constants Abstract methods Can not be instantiated
Must implement all methods in interface Example class foo implements bar { … } // interface 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
Autoboxing & Unboxing
Data value
Object (of matching class) Data types & classes converted Boolean, Byte, Double, Short, Integer, Long, Float
ArrayList myL = new ArrayList(); myL.add(1); // previously myL.add(new Integer(1)); Integer X = new Integer(2); int y = X; // previously int y = X.intValue(); Also see example in SortValues.java
Enumerated Types From "Taming the Tiger" presentation by Joshua Bloch and NealGafter at Sun's 2004 Worldwide Java Developer Conference public class Card implements Serializable { public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE } public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES } private final Rank rank; private final Suit suit; private Card( Rank rank, Suit suit ) { this.rank = rank; this.suit = suit; } public Rank rank( ) { return rank; } public Suit suit( ) { return suit; } public String toString( ) { return rank + " of " + suit; } }
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
Generics – Usage
Specify for utility class Automatically performs casts Can check class at compile time
class A { … } class B { … } List myL = new List( ); myL.add(new A( )); // Add an object of type A A a = myL.get(0); // myL element
class A … B b = (B) myL.get(0); // causes compile time error
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 myL = new List(); // compile time error // List used in place of List // List is not subtype of List