Object-Oriented Programming and Java Language Constructs - Notes | CMSC 132, Study notes of Computer Science

Material Type: Notes; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-5sr-1
koofers-user-5sr-1 🇺🇸

9 documents

1 / 36

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 132:
Object-Oriented Programming II
Object-Oriented
Programming & Java
Language Constructs
Department of Computer Science
University of Maryland, College Park
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24

Partial preview of the text

Download Object-Oriented Programming and Java Language Constructs - Notes | CMSC 132 and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Object-Oriented

Programming & JavaLanguage Constructs

Department of Computer Science

University of Maryland, College Park

Overview

Object-oriented programming (OOP)

Introduction to OOP principles

Java programming language

Review language constructs Introduce new language constructs Many from Java 5.

Techniques – Abstraction

Abstraction

Provide high-level model of activity or data

Procedural abstraction

Specify what actions should be performed Hide algorithms

Data abstraction

Specify data objects for problem Hide representation

Techniques – Encapsulation

Encapsulation

Confine information so it is only visible / accessiblethrough an associated external interface

Approach

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

Extension of abstraction

Java Programming Language

Language constructs designed to support OOP

Example Interface – supports procedure abstraction Class – supports encapsulation

Class libraries designed using OOP principles

Example Java Collections Framework Java Swing

Java Interface

Interface

Collection of Constants Abstract methods Can not be instantiated

Classes can implement interface

Must implement all methods in interface Example class foo implements bar { … } // interface bar

Similar to abstract class

But class can “inherit” from multiple interfaces

Overview

Object-oriented programming (OOP)

Introduction to OOP principles

Java programming language

Review language constructs Introduce new language constructs Many from Java 5.

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

New Java Language Constructs

Autoboxing Enumerated types Generics Enhanced for loop

Iterator interface

Stream input & output Scanner class Annotations BitSet class

Autoboxing & Unboxing

Automatically convert primitive data types

Data value

Object (of matching class) Data types & classes converted Boolean, Byte, Double, Short, Integer, Long, Float

Example

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

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

Generics – Usage

Using generic types

Specify for utility class Automatically performs casts Can check class at compile time

Example

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

Generics and subtyping

Even if class A extends class B List does not extend List

Example

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