Professional Documents
Culture Documents
List example
– new List returns an empty List, []
– list.add(a) adds a to the end of the list
[original_elements, a]
– get(x) returns the element at
position x in the List
– size() returns the size of the List
Information Hiding
Original motivation:
– Large programs have two special needs:
1. Some means of organization, other than simply
division into subprograms
2. Some means of partial compilation (compilation
units that are smaller than the whole program)
Obvious solution: a grouping of subprograms
that are logically related into a unit that can
be separately compiled (compilation units)
– These are called encapsulations
Encapsulation Constructs
Encapsulation in C
– Files containing one or more subprograms can
be independently compiled
– The interface is placed in a header file
Encapsulation in C++
– Similar to C
– Addition of friend functions that have access to
private members of the friend class
Encapsulation Constructs
C# Assembly
– Collection of files that appears to be a single
dynamic link library or executable
– Larger construct than class; used by all .NET
programming languages
Naming Encapsulations
Java Packages
– Packages can contain more than one class
definition; classes in a package are partial friends
– Clients of a package can use fully qualified name
or use the import declaration
Ada Packages
– Packages are defined in hierarchies which
correspond to file hierarchies
– Visibility from a program unit is gained with the
with clause
Class
An object's abilities
– Dogs: Snowy, Puppy
– Cats: Kitty
– Snowy.bark(), Puppy.bark(), Kitty.bark();
– Dog.bark()
Message Passing
Everything is an object.
A program is a bunch of objects telling each
other what to do by sending messages.
Each object has its own memory
Every object has a type (class).
All objects of a particular type (class) can
receive the same messages.
Class vs. Abstract Data Type
Encapsulation
Inheritance
Polymorphism
Language Examples
C++
– All of the class instances of a class share a single
copy of the member functions
– Each instance of a class has its own copy of the
class data members
– Instances can be in stack or heap
– Constructor and destructor
– Information hiding supported
Language Examples
Java
– Similar to C++, except:
All user-defined types are classes
All objects are allocated from the heap and accessed
through reference variables
Language Examples
C#
– Based on C++ and Java
– All class instances are heap dynamic
– Default constructors are available for all classes
– Garbage collection is used for most heap objects,
so destructors are rarely used
– structs are lightweight classes that do not support
inheritance
Object references
c: Circle
Created using
new Circle() radius = 1
Reference assignment
i 1 i 2 c1 c1
j 2 j 2 c2 c2
radius = 5 radius = 9
Inheritance
Inheritance
Inheritance
$e = new Entry();
$e.value = new Integer($INT.text); $e.type= 1;
Polymorphism and Dynamic Binding