Object Oriented Programming - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Object Oriented Programming

Description:

Originally written by Tom West, Holt Software. Edited by Jaye Herbert, Sir Frederick Banting S.S. ... Explain the importance of designing reusable code for ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 32
Provided by: tomwestmod
Category:

less

Transcript and Presenter's Notes

Title: Object Oriented Programming


1
Object Oriented Programming
  • A one hour introduction to O-O.
  • Originally written by Tom West, Holt Software
  • Edited by Jaye Herbert, Sir Frederick Banting S.S.

2
Grade 12 Expectations
  • Specific
  • Describe the difference between procedural and
    object-oriented programming
  • Describe how procedural and object-oriented
    programming paradigms can be used to solve
    different problems

3
Grade 12 Expectations
  • Explain the importance of designing reusable code
    for large software projects
  • Incorporate appropriate code from shared software
    libraries into software projects
  • Build and maintain a small software library to
    facilitate the reuse of code
  • Develop software libraries in project settings
  • Use predefined modules from software libraries to
    improve productivity

4
Why O-O?
  • Programs are getting too large to be
    comprehensible in-total by any human being
  • CS desperately looking for a way of cognitively
    managing super-large projects
  • Object Oriented paradigm allows programmers to
    use large blocks of code with a much smaller
    cognitive load
  • Makes code re-use a real possibility

5
Why O-O?
  • Benefits only occur in larger programs
  • Analogous to structured programming
  • Progs lt 30 lines, spaghetti is as understandable
    and faster to write than structured
  • Progs gt 1000 lines, spaghetti is
    incomprehensible, probably doesnt work, not
    maintainable
  • O-O greatest benefits are seen for programs of gt
    1000 lines

6
Why O-O?
  • Much of the benefit is in easier maintenance of
    large projects
  • Example of O-O strength
  • Write a library automation system handling books
    and periodicals.
  • Now you are given one week to modify program to
    handle video tapes, audio tapes and software!

7
What is O-O?
  • Cannot do anything using OO that cant be
  • done using procedural paradigm
  • Procedural Paradigm
  • Program defines data and then calls subprogram to
    act on the data
  • Object Paradigm
  • Program creates objects that encapsulate the data
    and procedures that operate on the data

8
OOP Concepts
  • A program models a world of interacting objects
  • Objects create other objects and send messages
    to each other (in Java, call each others
    methods)
  • Each object belongs to a class a class defines
    properties of its objects
  • A class implements an ADT the data type of an
    object is its class
  • Programmers write classes (and reuse existing
    classes)

9
Procedural vs. O-O
Procedural

Object 1

Object Oriented

Data

Object 2

Main

Program
Data


Object 3

Data

10
Basic Concepts
  • Objects
  • Hidden data that is accessed and manipulated
    through a well defined interface
  • Classes
  • A template or "blueprint" for creating objects,
    each with their own data
  • Inheritance
  • A new class created by modification of an already
    existing class

11
ObjectsLong Definition
  • Code containing a set of data, all the routines
    to manipulate the data, and an interface
  • The set of data are often called the fields of
    the object
  • The routines to manipulate the data are often
    called the methods of the object
  • The interface of an object is simply the subset
    of methods that other programs are allowed to
    call. The names and parameters of the methods in
    the interface should only rarely change

12
ObjectsDiagram
General Outline
Interface
imports Object Rest of the Program
method
method
Fields of The Object
method
method
method
method
method
PhoneBook Example

PhoneBook



imports PhoneBook PhoneBook Demo Program
Add
numEntries book (array of records

numEntries




Delete



book
(array


Find

of records)

LookUp

13
Objects Why Use Them? (1)
  • To use an object, the user need only comprehend
    the interface. No knowledge of the internals are
    necessary
  • Self-contained. Once the interface is defined,
    the programmer can implement the interface (write
    the object) without interference of others

14
Objects Why Use Them? (2)
  • Implementation can change at a later date without
    rewriting any other part of the program (as long
    as the interface doesn't change)
  • Changes in the data mean changing code in one
    location, rather than code scattered around the
    program (where it is easy to miss)

15
ObjectsProgramming
  • Hidden data that is accessed and manipulated
    through a well defined interface
  • PhoneBook
  • Hidden Data Set of names and phone s
  • Number of names in book
  • Interface add, delete, change, lookUp

16
Objects Interface
  • Why Hide the Data?
  • Allows implementer free hand to write and change
    the object
  • PhoneBook object
  • Allows user to enter, look up and delete names
    and phone numbers
  • Implemented using an array
  • Maximum 100 names in the phone book

17
ObjectsInterface Example
  • Example
  • The PhoneBook object is successful. It is used in
    hundreds of applications across the company
  • It only holds 100 records! It now must upgraded
    to hold unlimited number of records
  • How do we do so without breaking all the other
    programs in the company?

18
ObjectsInterface Example cont
  • The interface does not need to change. Thus there
    is no need to change any of the programs using
    PhoneBook object
  • If this had been programmed in the procedural
    paradigm, each program that used the phone book
    would have had a copy of the data array and would
    have to have been extensively modified to be
    upgraded

19
ObjectsInterface Example (4)
  • If we had given access to the fields of the
    object, other programs might have made use of the
    names, phones or numEntries fields and would
    break with new version of PhoneBook

20
ClassesLong Definition
  • Code looks superficially similar to a class
    ("static" is removed)
  • A class is a template. No data is allocated until
    an object is created from the class
  • The creation of an object is called
    instantiation. The created object is often called
    an instance (or an instance of class x)
  • No limit to the number of objects that can be
    created from a class
  • Each object is independent. Changing one object
    doesn't change the others

21
ClassesDiagram
Object 1
Fields for 1
Object 2
Main Program
Fields for 2
Object 3
Fields for 3
22
ClassesWhy Use Them?
  • A template or "blueprint" for creating objects,
    each with its own data
  • Without classes, each otherwise identical object
    must have its own object, differing only in name
  • Objects created (instantiated) from a class can
    be assigned to variables and manipulated as such.
    For example, a program can have arrays of objects

23
ClassesWhy Use Them?
  • In general, use classes instead of objects. Any
    number of objects can be created from a class and
    as variables, the objects created from a class
    are more flexible
  • Example
  • It may well turn out that one of the programmers
    wants to use two PhoneBook objects in the same
    program (separate PhoneBooks for professional and
    personal contacts). If we wrote it as a class,
    then no extra work is required on our part

24
ClassesProgramming
  • A template or "blueprint" for creating objects,
    each with their own data PhoneBook
  • Hidden Data
  • Set of names and phone s
  • Number of names in book
  • Interface
  • Add, Delete, Change, LookUp

25
Encapsulation
  • Means that all data members (fields) of a class
    are declared private. Some methods may be
    private, too.
  • The class interacts with other classes (called
    the clients of this class) only through the
    classs constructors and public methods.
    Constructors and public methods of a class serve
    as the interface to classs clients.

26
Inheritance
  • A new class created by modification of an already
    existing class
  • The inheriting class contains all the methods and
    fields of the class it inherited from plus any
    methods and fields it defines
  • The inheriting class can override the definition
    of existing methods by providing its own
    implementation
  • The code of the inheriting class consists only of
    the changes and additions to the base class

27
Inheritance Why Use Them? (1)
  • Frequently, a class is merely a modification of
    another class. In this way, there is minimal
    repetition of the same code
  • Fixing a bug in the base class automatically
    fixes it in the subclasses
  • Adding functionality in the base class
    automatically adds it in the subclasses

28
Inheritance Why Use Them? (2)
  • Less code makes the program easier to understand
  • There is less chance of different (and
    inconsistent) implementations of the same
    operation

29
InheritanceTerminology
  • Class one above
  • Parent class
  • Class one below
  • Child class
  • Class one or more above
  • Ancestor class, Base class
  • Class one or more below
  • Descendent class

30
Basic Concepts
  • Objects
  • Hidden data that is accessed and manipulated
    through a well defined interface
  • Classes
  • A template or "blueprint" for creating objects,
    each with their own data
  • Inheritance
  • A new class created by modification of an already
    existing class

31
Terms to Lookup
  • Overriding
  • Overloading
  • Polymorphism
  • super
  • this
  • clone
  • constructor
Write a Comment
User Comments (0)
About PowerShow.com