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

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

Pre 2010

Uploaded on 02/13/2009

koofers-user-8sk
koofers-user-8sk 🇺🇸

8 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
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

Partial preview of the text

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

CMSC 132:

Object-Oriented Programming II

Object-Oriented

Programming & Java

Language 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 / accessible through 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 specifies a contract Class implements/defines contracts, supports encapsulation of implementation

Class libraries designed using OOP principles

Example Java Collections Framework Java Swing

Java Interface

An Interface defines a contract

Collection of Constants Abstract methods; no implementations Can not be instantiated

Classes can implement interfaces

Must implement all methods in interface Example class Foo implements 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

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

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

Solution Generic Types

Generic types

Provides abstraction over types Can parameterize classes, interfaces, methods Parameters defined using notation

Examples

public class foo { … } List myNames = ...

Improves

Readability & robustness

Used in Java Collections Framework

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 aL = new LinkedList(); List bL = aL; // compile time error

Why?

Subtyping and generic types

Consider what could happen if legal

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)