Programming Languages - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Programming Languages

Description:

Abstraction X'is a view or representation. of entity X that includes only the ... Name is the class name, preceded by a tilde (~) Data Abstraction in C ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 43
Provided by: DavidGol3
Category:

less

Transcript and Presenter's Notes

Title: Programming Languages


1
ProgrammingLanguages
CSCI-4430 CSCI-6969April 1, 2008
  • David Goldschmidt, Ph.D.
  • Computer Science
  • The College of Saint Rose

2
Abstraction
  • Abstraction Xis a view or representationof
    entity X that includes only themost significant
    attributes of X
  • Abstractions combat program complexity
  • Abstraction X may be a subset of X oralso
    introduce a new interface

3
Process Abstraction
  • Nearly all programming languagessupport process
    abstraction with subprograms,functions, methods,
    etc.
  • Hide implementation details

// e.g. Java double result calculate( x1, x2
) ...
public static double calculate( double x1, double
x2 ) // ... calculate and return a double
// ... implementation details here ...
4
Data Abstraction
  • An abstract data type is a construct that
    provides
  • A type definition that allows program units to
    declare variables of the type, but hides the
    actual representation
  • A set of operations for manipulating objects of
    the type
  • Abstract data types can be built-in,
    user-defined, or available via libraries or
    packages
  • Nearly all programming languages designedsince
    1980 support data abstraction

5
Data Abstraction
  • Abstract data types satisfy the following
    conditions
  • 1. The representation of (and operations on)
    objects ofthe type are defined in a single
    syntactic unit
  • Actual implementation (i.e. code) may be
    elsewhere...
  • Advantages?
  • 2. The representation of objects of the type is
    hidden from program units that use these
    objects, exposingonly those operations provided
    in the types definition
  • Advantages?

6
Data Abstraction Example
  • Consider an abstract data type that representsa
    collection of elements with operations
  • create(collection, maxSize)
  • destroy(collection)
  • add(collection, element)
  • remove(collection, element)
  • isEmpty(collection)
  • isFull(collection)
  • size(collection)

7
Data Abstraction Example
  • Clients use a types abstract interface tomanage
    collections ofpotentially varied data
  • Is coll an array?
  • Is coll a linked list?
  • Is coll a hash table?

create(coll, 20) while (not isFull(coll)) //
input student info ... add(coll,
student) if (isFull(coll)) display("Course
is full!")
8
Data Abstraction Example
  • We can define one abstract data type based on
    another to focus or restrict its use e.g. a
    stack
  • create(stack, maxSize)
  • destroy(stack)
  • push(stack, element)
  • pop(stack)
  • isEmpty(stack)
  • isFull(stack)
  • size(stack)

9
Data Abstraction in C
  • In C and C, use struct for data abstraction
    requiring only data
  • In C, a class defines an abstract data type
    typically with both data and operations
  • Data members are the data
  • Member functions are the operations

10
Data Abstraction in C
  • Define data members and member functions as
    either class members or instance members
  • A class member is associated with the class
  • An instance member is associated with objects
  • All class instances share a single copyof the
    member functions
  • Each instance of a class has its own copyof the
    class data members
  • Instances can be static, stack dynamic, or heap
    dynamic

11
Data Abstraction in C
  • Instances can be stack dynamic or heap dynamic
  • If stack dynamic, class instances are
    referenceddirectly with value variables
  • If heap dynamic, classinstances are
    referencedvia pointers
  • Advantages and disadvantages?

main() stack stk stk.push(21)
stk.push(18) cout ltlt stk.pop() ...
main() stack stk stk new stack
stk-gtpush(21) stk-gtpush(18) cout ltlt
stk-gtpop() ...
12
Data Abstraction in C
  • Constructors are functions that initialize
    thedata members of class instances
  • Initialization might allocate storage (via
    new)if data members are heap-dynamic
  • Implicitly called when an instance is created
  • Can also be explicitly called
  • Name is the same as the class name

13
Data Abstraction in C
  • Destructors are functions that clean up aftera
    class instance is destroyed
  • Typically used to reclaim heap storage (via
    delete)
  • Implicitly called when the objects lifetime ends
  • Can also be explicitly called
  • Name is the class name, preceded by a tilde ()

14
Data Abstraction in C
  • Information hiding in C is supportedvia
    visibility clauses
  • public defines visible entities
  • private defines hidden entities
  • protected defines hidden entities available for
    use in child classes

15
Data Abstraction in C
class stack private int stackPtr
int maxLen int topPtr public
stack() // default constructor stackPtr
new int100 maxLen 99 topPtr
-1 stack () delete stackPtr
void push(int number) ... int pop()
... int peek() ... int isEmpty()
...
16
Data Abstraction in C
  • Friend functions and friend classes
    provideaccess to private members to
    unrelatedprogram units or functions
  • Allows for multiple classes to view and
    modifyone anothers private data members and
    functions

17
Data Abstraction in Java
  • Java is similar to C, except
  • All user-defined types are classes
  • i.e. no structs
  • Java also supports pure abstractions called
    interfaces
  • All objects are allocated from the heap
    andaccessed through reference variables
  • Individual entities in classes have access
    control modifiers (private or public) rather than
    clauses

18
Data Abstraction in Java
class Stack private int stack private
int maxLen, topIndex public Stack() //
default constructor stack new int100
maxLen 99 topIndex -1 public
void push(int number) ... public int
pop() ... public int peek() ...
public boolean isEmpty() ...
Stack s // reference variable s new
Stack() s.push(21) s.push(18) System.out.print(
s.pop()) ...
19
Data Abstraction in Java
  • Use static to define methods that are available
    without creating an instance of an object
  • e.g. Math.random(), System.out.println(), etc.
  • Omit static to define instance methods that are
    available only after an object is instantiated
  • Circle c1 new Circle(12.5)
  • double area c1.getArea()

20
Java Garbage Collection
  • Objects that are no longer referenced are garbage
  • The Java Virtual Machine ( JVM ) automatically
    performs garbage collection
  • Frees up memory used by garbage
  • Usually sufficient to allow JVM to
    performgarbage collection at its own pace
  • To force garbage collection on an object,assign
    its reference variable to null
  • Can also try calling System.gc()

21
Visibility Modifiers in Java
  • Visibility modifiers can be applied toany class,
    variable, or method
  • Using private, the class, variable, ormethod can
    only be accessed by the declaring class
  • Using public, the class, variable, or method
    canbe accessed by any class in any package
  • Omitting the modifier, the class, variable, or
    methodcan be accessed by any class within the
    same package

22
Object-Oriented Programming
  • Object-oriented programming is aprogramming
    paradigm that combines
  • Abstract data types
  • Inheritance (a.k.a. generalization)
  • Dynamic binding (a.k.a. polymorphism)
  • Languages often provide OOP in addition to
    procedural and other language constructs

23
Inheritance
  • Much of our world is organized(or organizes
    itself ) into hierarchies
  • Inheritance is used to modelparent-child
    relationships
  • Key benefits are organization and reuse
  • e.g. Bank accounts, geometric shapes, people,
    etc.
  • BankAccount ? CheckingAccount
  • Polygon ? Rectangle ? Square
  • Person ? Student ? GraduateStudent

24
Inheritance
  • UML diagram representing Generalization
  • Children classes are more specific
  • Parent classes are more general

25
Inheritance
  • A class that inherits methods and data membersis
    called a derived class, a subclass, or a child
    class
  • The class from which another class inheritsis a
    parent class or a superclass
  • Methods define operations on data members
  • Calls to methods are often called messages
  • A class inherits all or some of the entitiesof
    its superclass
  • e.g. in Java, constructors are not inherited

26
Inheritance
  • A subclass can modify an inherited method
  • The new method overrides the inherited one
  • Overriding inherited methods provides formore
    specific operations in the subclass
  • Similar to determining scope of variables,when a
    method is called,its specific data type (i.e.
    its class)must be determined

27
Polymorphism
  • The Substitutability Principle
  • Given an inheritance relationship, we can use
    achild element anywhere the parent is expected
  • We can use the morespecific element anywherethe
    more general elementis expected

28
Polymorphism
  • Polymorphic operations have the same
    signature,but different implementations

29
Polymorphism
  • Polymorphism allows you to send the same message
    to different classes (and the objects respond
    appropriately)

Canvas
r1.draw()
r1Rectangle
c2.draw()
c2Circle
c3.draw()
c3Circle
30
Dynamic Binding
  • Polymorphism is achieved via dynamic binding
  • More specifically, dynamically bind messagesto
    method definitions at run time
  • Based on class hierarchies
  • Advantages and disadvantages
  • Has the advantage of being easily extensible
  • Improves writability of reusable methods
  • Key disadvantage is run time performance

31
Dynamic Binding in C
public class shape public // pure virtual
function virtual void draw() 0
... public class circle public shape
public void draw() ... ... public
class rectangle public shape public
void draw() ... ... public class square
public rectangle public void draw()
... ...
square sq new square rectangle r new
rectangle shape sptr // Cannot create a shape
object sptr new shape // Point sptr to the
square object sptr sq // Dynamically bind to
draw() // method of the square
class sptr-gtdraw() // Statically bind to
draw() // method of the rectangle
class r-gtdraw()
32
Dynamic Binding in Java
public class Shape public void draw() // do
nothing ... public class Circle extends
Shape public void draw() ...
... public class Rectangle extends Shape
public void draw() ... ... public class
Square extends Rectangle public void draw()
... ...
Square sq new Square() Rectangle r new
Rectangle() Shape shape // Cannot create a
shape object shape new Shape() // Point shape
to the square object shape sq // Dynamically
bind to draw() // method of the square
class shape.draw() // Statically bind to
draw() // method of the rectangle class r.draw()
33
Interfaces in Java
public interface Shape public void draw()
... public class Circle implements Shape
public void draw() ... ... public class
Rectangle implements Shape public void draw()
... ... public class Square extends
Rectangle public void draw() ... ...
Square sq new Square() Rectangle r new
Rectangle() Shape shape // Cannot create a
shape object shape new Shape() // Point shape
to the square object shape sq // Dynamically
bind to draw() // method of the square
class shape.draw() // Statically bind to
draw() // method of the rectangle class r.draw()
34
Interfaces (Abstract Base Classes)
  • An interface consists of a set of public
    operations that have no implementation

35
Interfaces (Abstract Base Classes)
  • Why use interfaces?
  • Readability Interfaces are typically very
    concise
  • Reusability Interfaces provide a contract that
    many classes may implement to realize a service
  • Flexibility of Implementation Interfaces define
    only operation signatures and hint at semantics
  • Architectural Construct Interfaces represent
    architectural seams, separating specification
    from implementation
  • Ease of Integration Sharing interfaces with
    other systems

36
Example Interfaces
  • Interface names often end in the -able suffix

37
Example Interfaces
  • More interfaces that end in the -able suffix

38
Example Interfaces
  • Interfaces that dont end in the -able suffix

39
Example Interfaces
  • More interfaces that dont end in the -able
    suffix

40
HW 4/1
  • Suppose the abstract data type Stack provideda
    peek() function that returned an access path
    (i.e. a pointer or reference) to the top
    elementof the stack
  • Why is this not a true data abstraction?
  • What is the purpose of Java (and Python) classes
    having a common ancestor (i.e. the Object class)?

41
HW 4/1
  • Use Java, C, C, or another languagewith OOP
    support to create a Queue classfor integers
  • Be sure that the class only allows for elements
    to be added to the end of the queue
  • Further, elements may only be viewed or removed
    from the beginning of the queue
  • Create an entirely static version of the
    class,then create an entirely dynamic version of
    the class
  • Modify your Queue class to support elements of
    any data type

42
Reading Assignments
  • Read for this week
  • Chapters 11-13
Write a Comment
User Comments (0)
About PowerShow.com