Sebesta – Concepts of Programming Languages

High Level 2 – Summary Notes for Chapter 11, 12 & 14

Chapter 11 – Abstract Data Types and Encapsulation Constructs

Core idea: Data abstraction = hide how data is represented, expose only operations. ADTs and encapsulation are language mechanisms to support this.

11.1 The Concept of Abstraction

Abstraction = ignoring irrelevant details and focusing on the essential properties of an entity or operation.

Two main kinds of abstraction:

11.2 Introduction to Data Abstraction

Data abstraction means that a data type is defined by:

An Abstract Data Type (ADT) is a data type where:

Benefits of data abstraction:

11.3 Design Issues for Abstract Data Types

Key questions language designers must answer about ADTs:

11.4 Language Examples (ADT Support)

C++

Uses class as an ADT mechanism.

// Example C++ ADT
class Stack {
private:
    int* data;
    int topIndex;
public:
    Stack(int capacity);
    ~Stack();
    void push(int value);
    int pop();
    bool isEmpty() const;
};

Java

C#

Ruby

Python

11.5 Parameterized Abstract Data Types (Generics)

Parameterized ADT = ADT that takes a type parameter.

Examples:

Different implementations:

Benefits: reusability, type safety, no need for “void*” or casts, fewer runtime errors.

11.6 Encapsulation Constructs

Encapsulation = grouping related declarations and hiding implementation details.

Examples:

11.7 Naming Encapsulations

This covers how encapsulated units (modules, packages, namespaces) are named and referenced.

Examples:


Chapter 12 – Support for Object-Oriented Programming

Core idea: OOP = ADTs + inheritance + dynamic binding. This chapter explains what “object-oriented” really means in language design.

12.1 Introduction

OOP is built on the foundations of data abstraction (Chapter 11), plus:

Languages differ in how “purely” object-oriented they are:

12.2 Object-Oriented Programming

A language is typically considered object-oriented if it supports:

12.2.2 Inheritance

Inheritance allows a new class (subclass) to reuse and extend an existing class (superclass).

Motivations for inheritance:

Inherited members can be:

12.3 Design Issues for Object-Oriented Languages

Key design concerns include:

12.4 OOP in Specific Languages

Smalltalk

C++

Objective-C

Java

C#

Ruby

12.5 Implementation of OOP Constructs

How languages implement OOP features internally:

12.6 Reflection

Reflection = a program can inspect or modify its own structure at runtime.

Typical capabilities:

Supported in:


Chapter 14 – Exception Handling and Event Handling

Core idea: Exception handling = reacting to unusual conditions (errors or not) in a structured way. Event handling = reacting to external events (usually GUI user actions).

14.1 Introduction to Exception Handling

Exception = any unusual event (error or not) that is detectable by hardware or software and may require special processing.

Examples:

Exception handler = code segment executed when an exception occurs.

Raising (or throwing) an exception = signaling that an exceptional condition has occurred.

Without built-in exception handling

Older approaches:

Problems: code gets cluttered, logic becomes messy, error-handling is inconsistent.

Advantages of built-in exception handling

14.2 Exception Handling in C++

C++ supports exceptions with:

Issues:

14.3 Exception Handling in Java

Java has a more disciplined exception model.

Hierarchy

Syntax

try {
    // code that may throw
} catch (SomeException e) {
    // handler
} finally {
    // code that always runs
}

Checked vs unchecked exceptions:

finally is executed regardless of how the try block exits, used for cleanup.

Assertions in Java (14.3.7)

Used for defensive programming.

assert condition;
assert condition : "message";

If the condition is false → AssertionError is thrown. Assertions can be enabled/disabled at runtime with JVM flags.

Evaluation of Java’s design (14.3.8)

14.4 Exception Handling in Python and Ruby

Python (14.4.1)

In Python, exceptions are objects.

try:
    # code
except Exception1:
    # handler
except Exception2:
    # handler
else:
    # runs if no exception
finally:
    # always runs

Differences from Java:

raise is used to trigger an exception:

raise IndexError
raise ValueError("bad value")

assert is a conditional raise of AssertionError and can be disabled with the -O flag.

Ruby (14.4.2)

Raising exceptions:

raise "bad parameter" if count == 0
raise TypeError, "Float parameter expected" if !param.is_a?(Float)

Handling exceptions:

begin
  # code
rescue
  # handler
else
  # if no exception
ensure
  # always runs (like finally)
end

Ruby allows retry at the end of a handler to re-run the protected code.

14.5 Introduction to Event Handling

Event = notification that something external has occurred (e.g. GUI interaction).

Event handler = code that runs when an event occurs.

Event-driven programming:

Examples:

14.6 Event Handling with Java

Java uses Swing components and listener interfaces.

GUI Elements (Widgets)

Event Listeners

14.7 Event Handling in C#

C# uses .NET’s Windows Forms and the event/delegate model.

Delegates are type-safe function pointers that define the protocol for event handlers.