You are on page 1of 39

Principles of Programming Languages

Abstract Data Types and Classes


"Get your data structures correct
first, and the rest of the
program will write itself."
- David Jones
Outline

 Development of large software


 Abstraction
 Abstract Data Type
 Class and Objects
 OOP
 Inheritance
 Polymorphism
Software Development

 Consider the objective of constructing a large


software system
– There are difficulties due to the fact that we will have to deal
with a large number of data objects and functions
 To successfully develop such software systems, you
need to consider two important aspects
– Data structures to hold data
– Algorithms to manipulate these structures
 Program = algorithm + data structure
Software Development (2)

 To reduce the complexity of development and


maintenance, the concept of abstraction is used
– this involves specifying the functionality of the software
system in high-level terms during design
 Two type of abstraction are typically used
– Procedural abstraction
 the separation of what a function/module does from how the
function accomplishes its purpose.
– Data abstraction
 separation of the logical view of a data object (what is stored)
from the physical view (how the information is represented).
Abstract Data Type (ADT)

 An abstract data type (ADT) is:


– a collection of data, along with
– A set of operations on that data.
 ADTs are independent of implementation,
and even of programming language.
ADT Examples

 Example: The SET ADT is a collection of


data items that are accessed by operations
such as
– Union, Intersection, and Set Difference
 Example: String ADT is an ordered sequence
of characters with operations
– concatenate, length, compare, substring, etc.
Example

 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

 An ADT must obviously have some kind of


representation for its data
– The user need not know the representation
– The user should not be allowed to tamper with the
representation
– Solution: Make all data private
 But what if it’s really more convenient for the
user to have direct access to the data?
– Solution: Use setters and getters
Encapsulation Constructs

 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

 Large programs define many global names;


need a way to divide into logical groupings
 A naming encapsulation is used to create a
new scope for names
 C++ Namespaces
– Can place each library in its own namespace and
qualify names used outside with the namespace
– C# also includes namespaces
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

 A class defines the abstract


characteristics of a thing (object),
– Characteristics (attributes)
– Thing's behaviors (methods).
Classes and Objects

 Object is a particular instance of a class.


 Class Dog
– breed and fur color (characteristics)
– ability to bark (behavior).
 Object Snowy
– tiny dog
– white fur
 In programmer jargon, the object Lassie is an
instance of the Dog class.
Object Interface
Light
On()
Off()
 An object has to have an interface.
Brighten()
 The interface will Dim()
– Provide a way to let other objects to communicate
with it.
– Hide the details of the object implementation.
Object Hiding Information
Method

 An object's abilities
– Dogs: Snowy, Puppy
– Cats: Kitty
– Snowy.bark(), Puppy.bark(), Kitty.bark();
– Dog.bark()
Message Passing

 Message passing is the process by which an


object sends data to another object or asks
the other object to invoke a method.
– Kitty.scratch(Snowy)
Message Passing

 Message passing is the process by which an


object sends data to another object or asks
the other object to invoke a method.
– Kitty.scratch(Snowy)
 Snowy.bark()
Message Passing

 Message passing is the process by which an


object sends data to another object or asks
the other object to invoke a method.
– Kitty.scratch(Snowy)
 Snowy.bark()
 Snowy.chase(Kitty)
Message Passing

 Message passing is the process by which an


object sends data to another object or asks
the other object to invoke a method.
– Kitty.scratch(Snowy)
 Snowy.bark()
 Snowy.chase(Kitty)
– Kitty.hide()
Message Passing

 Message passing is the process by which an


object sends data to another object or asks
the other object to invoke a method.
– Kitty.scratch(Snowy)
– Snowy.sleep()
Message Passing

 Message passing is the process by which an


object sends data to another object or asks
the other object to invoke a method.
– Kitty.scratch(Snowy)
– Snowy.sleep()
– Kitty.eat()

 Script-based <> event-based


Two paradigms of programming

- Data structures + Algorithms = Program


- Objects + Messages = Program
Object Oriented Programming

 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

 Class == Encapsulated ADT?


Class = Encapsulated ADT++

 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

Primitive type int i = 1 i 1

Object type Circle c c reference

c: Circle
Created using
new Circle() radius = 1
Reference assignment

Primitive type assignment Object type assignment


i=j c1 = c2

Before: After: Before: After:

i 1 i 2 c1 c1

j 2 j 2 c2 c2

c1: Circle c2: Circle

radius = 5 radius = 9
Inheritance

Superclass Circle Circle Methods Circle Data

Inheritance

Cylinder Circle Methods Circle Data


Subclass
Cylinder Methods Cylinder Data
Inheritance

Superclass Circle Circle Methods Circle Data


Private

Inheritance

Cylinder Circle Methods Circle Data


Subclass
Cylinder Methods Cylinder Data
Subclass

 Add new properties


 Add new methods
 Override the methods of the superclass
Superclass and subclass

public class Entry


{
public int type;
public Object value;
}

$e = new Entry();
$e.value = new Integer($INT.text); $e.type= 1;
Polymorphism and Dynamic Binding

 Class B { int f(); } extends A { int f(); int f2(); }


A a = new B;
a.f()  JMP to If

xxx.f()  JMP to If ?

Static binding vs. dynamic binding

You might also like