03JavaConstructs

03JavaConstructs - CMSC 132: Object-Oriented Programming II...

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

View Full Document Right Arrow Icon
1 CMSC 132: Object-Oriented Programming II Object-Oriented Programming & Java Language Constructs Department of Computer Science University of Maryland, College Park
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Review of Java Language Constructs Basic elements Primitive types, variables, constants, operators If-else, switch, while, for Classes Object instances Creating objects with new Object references The null reference Instance data, class (static) data Methods Parameters, return values, polymorphism
Background image of page 2
3 Review of Java Language Constructs Inheritance Base class, derived class, super Method overriding (vs. overloading) Abstract methods Up- and down-casting , getClass() , instanceof avoid overuse of these. .. leads to bad designs Interfaces 1D Arrays Creating, indexing Exceptions Try-catch blocks
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 New Java Language Constructs Autoboxing Enumerated types Generics Enhanced for loop Iterator interface Scanner class Annotations BitSet class
Background image of page 4
5 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
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Enumerated Types From "Taming the Tiger" presentation by Joshua Bloch and Neal Gafter 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; } }
Background image of page 6
7 Generics – Motivating Example Problem Utility classes handle arguments as Objects Objects must be cast back to actual class Casting can only be checked at runtime Example 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
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Solution – Generic Types
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 28

03JavaConstructs - CMSC 132: Object-Oriented Programming II...

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

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