159.331 Programming Languages - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

159.331 Programming Languages

Description:

We need to discuss some design ideas first to fully appreciate what OO is ... Not all language s allow this, and it should certainly be used cautiously. 159.331 ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 21
Provided by: KenHa98
Category:

less

Transcript and Presenter's Notes

Title: 159.331 Programming Languages


1
159.331 Programming Languages Algorithms
  • Lecture 11 - Object Oriented Languages Part 1

2
OO Design Methodology
  • We need to discuss some design ideas first to
    fully appreciate what OO is all about
  • Software design - waterfall method
  • Specification - hardly ever right first time (if
    ever!)
  • Design - top-down functions, then subtasks, then
    sub-subtasksuntil trivial cases
  • Implementation
  • Test
  • Do it all again (iterate or refine)
  • Although sometimes better than more ad hoc
    bottom up approach, top-down functional still has
    problems
  • Inflexible, hard to reuse code, high maintenance
    costs, the requirements (always)
    changeAccommodate change?

3
So some Bottom-Up design might be good?
  • If we have some well chosen objects (or
    components) or just pieces of code maybe
    they capture the key ideas and can be reused
  • OO came about because of need to address these
    problems
  • Need to stay flexible (accommodate change)
  • Want to reuse code (saves time, money, effort)
  • Need to integrate the design phases neatly -
    common models and notations (languages)

4
Object-Oriented Design
  • Focus on data objects rather than functions
  • Look for the right objects that capture the
    essence of the system
  • Specification changes tend to affect the objects
    less than the functions so OO software less
    vulnerable to specification changes
  • Also OO lets us hide representation and
    implementation details - hopefully lowering
    complexity
  • Phase integration easier too - all phases deal
    with the same thing - ie objects

5
OO Language Ideas
  • The operations on an object constitute its (user)
    interface
  • Data encapsulation hides details - like ADTs
  • Inheritance allows us to specify relationships
    between certain sorts of objects (hierarchies)
  • Dynamic binding allows us to delay binding until
    run time - choice of what code to execute is made
    at run-time, rather than at compile time

6
Classes
  • A class is a kind or category of object
  • Classes contain
  • Description of the internal variables of objects
    of this class
  • The operations that can be applied to objects of
    this class
  • The class concept shares ideas from modules and
    types - it is essentially an Abstract Data Type
    that supports inheritance
  • The class is the recipe for instantiating objects

7
Example stack class in C that only supports
ints - could use a generic to support arbitrary
types.
We can have stack s
s new stack s
-gt push(25) int x
s-gtpop() Notice that s-gttop is not allowed,
as top is protected data C also allows us to
declare directly as in stack s
s.push(25) Not all OO languages allow this use
of objects.
8
The actual code implementation has to be
specified somewhere. In C we use the scope
resolution operator to give the full names
In C we are allowed to mix up specification and
implementation Not all language s allow this,
and it should certainly be used cautiously
9
Object Initialisation and Deletion
  • We can supply code to be invoked when an object
    is created (instantiated) - known as its
    constructor
  • Also code that is invoked when is is destroyed
    (known as its destructor)
  • Constructors usually used to set up any memory
    the object needs
  • Destructor usually used to tidy up afterwards
  • Note that constructors and destructors are
    designed to be called by the system, NOT
    directly by the user. A bit like handlers in
    that sense.

10
(No Transcript)
11
(No Transcript)
12
Inheritance
  • Allows us to define classes by extending existing
    ones
  • We can extend the internal data structures and
    reuse operations code
  • The new class is called a derived or child
    class
  • The pre-existing class is called the base or
    parent class

13
In C we use the operator to say
extends
14
Notice we pass a size argument through to the
constructor, So our stack can hold (at most) 500
elements
  • Our notation is like PostScript example
  • 2 3 add
  • yields 5 on the stack, which we can pop off
    the stack

15
Inheritance and Class Hierarchies
  • The inheritance idea lets us build quite
    complicate d class hierarchies, without having to
    rewrite all the code for each class from
    scratch.
  • For example a publications database might have
    classes for Books, Newspapers, Magazines.
  • They share some data and operations and have
    their own specific extra data and operations eg
    some have authors, some editors,

16
FTPReport has over-ridden the operation medium
17
Inheritance and Types
  • In some languages classes are types
  • In C a class is essentially a user-defined
    type
  • We can view a derived class as a subtype of the
    base class

18
Note that r-gtSupervisor() is illegal (at compile
time)
19
Class-Objects
  • Not all languages consider classes to be types -
    since redefined (overridden) operations could
    do something completely arbitrary compared to the
    original base types operation
  • Smalltalk-80 for example considers a class to be
    an object (!)
  • Every Smalltalk class-object must include at
    least one to create new instances of the class
  • This is a subtle idea - the class could be
    treated as a first-class data entity in a
    language and modified at run time. (C does not
    allow this, neither does Java although the
    concepts associated with JavaBeans let you do
    some data manipulations on classes)

20
OO Part 1 - Summary
  • OO Design
  • Classes
  • Inheritance
  • Class Hierarchies
  • Bal and Grune Chapter 3 - Sections 3.1-3.5
  • Sebesta Chapters 11 and 12
  • Next Polymorphism and Dynamic Binding
Write a Comment
User Comments (0)
About PowerShow.com