Abstraction and Objects Procedural Abstraction Procedural Abstractions organize
Abstraction and Objects
Procedural Abstraction • Procedural Abstractions organize instructions. Function Power Give me two numbers (base & exponent) I’ll return to you baseexponent ? ? ? Implementation ? ? ?
Data Abstraction • Data Abstractions organize data. Student. Type Name (string) GPA (num) Letter Grade (char) Student Number (num)
Behavioral Abstraction • Behavioral Abstractions combine procedural and data abstractions. Queue Object Enqueue Is Full Data State Is Empty Dequeue Initialize
The Object-Oriented Paradigm • Instances of behavioral abstractions are known as objects. • Objects have a clear interface by which they send and receive messages (communicate). • OO is a design and approach issue. Just because a language offers object-oriented features doesn’t mean you’re doing OO programming.
Information Hiding means that the user has enough information to use the interface, and no more Example: Stick shift We don’t care about the inner workings. . . We just want to get the car going! 1 3 2 4 5 R
Encapsulation is the grouping of related things together within some structure Item 1 Item 2 Item 3
Encapsulation via Algorithms encapsulate modules, data, and instructions. Algorithm Instructions Procedure Function Data
Encapsulating Instructions Modules encapsulate instructions. Procedure/Function Instructions Module call Instructions
Encapsulating Data Records allow us to do this with data Record Field 1 Field 2 Record
Revisiting the Question “Where is the Queue? ” • Notice we still have no way of identifying the idea we’re discussing… – The Queue is still in the “ether. ” • We’d like to encapsulate the data (regardless of it’s actual representation) with the behavior (modules). • Once we do this, we’ve got a logical entity to which we can point and say, “there it is!”
Encapsulating Data with Methods Abstract data types (ADTs) allow us to encapsulate logically related data and modules Queue Enqueue Data Dequeue
Abstract Data Types • An idea, a concept, an abstraction of the “big picture” Queue Enqueue Is Full Data State Is Empty Dequeue Initialize
Achieving Behavioral Abstraction • Abstract data types (ADTs) are concepts. • We require some way to implement these common abstractions so we can write them once, verify that they are correct, and reuse them. • This would save us from having to re-do work. For example, every time we create a queue we did: List_Node definesa. . . q_front isoftype. . . q_tail isoftype. . . procedure Enqueue(. . . ) procedure Dequeue(. . . ) • We need an algorithmic construct that will allow us to bundle these things together… the class.
OO Summary • Behavioral abstraction combines data abstraction with procedural abstraction. • The object-oriented paradigm focuses on the interaction and manipulation of objects. • An Abstract Data Type (ADT) allows us to think of what we’re representing as a thing regardless of it’s actual implementation.
Summary: Classes and Objects • Java like many OO languages has a way of describing an abstract data type (that is something encapsulating both data and methods for manipulating the data) • This structure is known as a class – It is sort of like a record which contains procedures in addition to the data – Thus it is a definition of a type • When we actually “make” an instance of one of these classes we get an object.
- Slides: 17