Object-Oriented Programming


Summary: This document contains my lecture notes for the object-oriented discussion in CSE341. These notes are derived from Craig Chambers' lecture notes on object-oriented programming.

Basics

Object-oriented programming is built on the following.

Here are some historically significant object-oriented languages.


Abstract Data Types

Abstract data types are user-defined data structures along with user-defined operations on these data structures. ADTs hide implementation and representation from the clients, thus the implementation and representation may be changed with out affecting the clients.

ADTs are called classes in object-oriented languages.

ADT Example: a stack class

ADT Benefits

ADT "Deficits"??


Inheritance

Inheritance allows for the definition of a new class as an incremental modification of an existing class.

The new class is called a subclass of the original class (which is called a superclass of the new class).

Subclass gets the instance variables and methods of the superclass.

Inheritance Example: Rectangles

Inheritance Benefits

Inheritance achieves code sharing by factoring code into common superclasses

May model real world scenarios well?

Inheritance Deficits

Inheritance is often over used by novices

Code gets fragmented into small factored pieces (this is good and bad)

Tracing control logic of code is harder

Perhaps inheritance with only extensions and overriding is too limited?


Dynamic Binding

Dynamic binding allows subclass to be used wherever a superclass is expected, thus reusing superclass's code for subclass.

A subclass can be assigned to a variable of superclass type.

r:Rectangle := . . .;
cr:ColoredRectangle := . . .;
. . . 
r := cr; 

When invoking operations on an object, the run-time system locates and invokes the right operation for the object.

r.draw(); // invokes draw for ColoredRectangle

This is called subtype polymorphism.

Dynamic binding requires run-time class information for each object.

Dynamic binding is also known as: message passing, virtual function calling, or generic function calling.

Method Lookup

What happens at run-time to implement dynamic binding? Consider the following code: obj.msg(args).

Start with the run-time class C of obj (the receiver)

If a match is never found, report a run-time error.

Dynamic binding is similar to overloading in the following ways.

Dynamic binding is unlike overloading in the following ways.

Example: Displaying shapes in a list

Dynamic Binding Benefits

Dynamic binding allows subtype polymorphism and class-specific methods.

Dynamic binding allows subclasses to be added without modifying clients.

Dynamic Binding Deficits

Dynamic binding can make the logic of the program harder to follow and reason about.

Dynamic binding adds some run-time overhead.


echris@cs.washington.edu (30 November 1996)