OOP - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

OOP

Description:

Discuss a PICA (Polymorphic, Inheritance, and Composition Architecture) ... NOTE: All of these applications could actually be one application with a PICA. ... – PowerPoint PPT presentation

Number of Views:370
Avg rating:3.0/5.0
Slides: 32
Provided by: RJS
Category:
Tags: oop | pica

less

Transcript and Presenter's Notes

Title: OOP


1
OOP
  • Chapter 11 Overview

2
Todays Lesson Plan
  • Discuss labs, quizzes, and TDA topics (10 min)
  • One more week left so please keep up!
  • Class Activities covers Chapter 11 (120 min)
  • Discuss a PICA (Polymorphic, Inheritance, and
    Composition Architecture)
  • Discuss and visually show the structure, syntax
    and execution of an Inheritance application.
  • Discuss and visually show the structure, syntax
    and execution of a Polymorphic Application.
  • Discuss and visually show the structure, syntax
    and execution of a Composition application.
  • NOTE All of these applications could actually be
    one application with a PICA.
  • Coding Exercises break (90 min)
  • Code Ex 1 p 594 11.3.8a 8b
  • Code Ex 2 p 601 11.4.5
  • Code Ex 3 p 601 11.4.5

3
Objectives
  • Additional Class Features
  • Class Inheritance Polymorphism
  • Reference Variables as Class Members
  • The this reference
  • Memberwise assignment

4
Each object has its own copy of the Class
instance variables
  • Each object maintains its own set of instance
    variables
  • Permits each object to have its own clearly
    defined state
  • When an object is created, a distinct area of
    memory is set aside for its instance variables

5
The this Reference
  • Member methods
  • Unlike data storage
  • Only one copy of each member method is maintained
    in memory per class
  • Requires providing a means of identifying which
    specific object a member method should be
    operating on
  • Use the this reference to specify the current
    instance

6
Memberwise Assignment
  • Using the operator to assign objects results in
    a shallow copy
  • Memberwise assignment
  • Also called deep copy
  • Assigns each instance variable a value
    specifically

7
Memberwise Assignment (continued)
  • Cascading method call
  • Example
  • a.setEqual(b.setEqual(c))
  • Method must return same data type as parameter
  • Can use the this reference in a return statement

8
Class Inheritance Polymorphism
  • Inheritance
  • Constructing one class from another
  • E.g., constructing a Sphere from a Circle
    class
  • Polymorphism
  • Provides the ability to redefine how methods of
    related classes operate based on the object being
    referenced

9
Inheritance
  • Capability of deriving one class from another
    class
  • Base class or Original class
  • Also called Parent or Superclass

10
Derived Class Properties
  • Derived class
  • Derived from base
  • Also called
  • Child or Subclass
  • Completely new class
  • Incorporates all the variables and methods of the
    base class
  • Adds new data and method members
  • Can override any base class method

11
Kinds of Inheritance
  • Simple inheritance
  • Each derived type has only one immediate base
    type
  • Multiple inheritance
  • Derived type has two or more base types
  • Not supported in Java
  • Class hierarchies
  • Illustrate the order in which one class is
    derived from another

12
Inheritance Diagrams
  • Illustrate the relationship between a base class
    and one or more derived classes
  • Base class is always drawn at the top of the
    diagram (top-down)
  • Derived classes are drawn under the base class

13
(No Transcript)
14
Inheritance Syntax Idioms
  • Header line syntax for derived class
  • public class derivedClassName extends
    baseClassName
  • protected visibility
  • Behaves identically to private visibility within
    a class
  • But it permits this restriction to be inherited
    by any derived class
  • super reserved word
  • Refers to a method or variable in the parent
    class

15
Abstract Classes and Interfaces
  • Abstract class
  • Can only be used as a base class for another
    class
  • Can not be instantiated
  • Created using the abstract reserved word in the
    class header
  • Typically contains one or more abstract methods
  • Method does not contain a body, only a header
  • Derived classes must define abstract methods
    implementation
  • Class can also include non-abstract methods

16
Abstract methods cannot be
  • Abstract methods cannot be declared as
  • static or final
  • final not allowed because it doesnt allow
    over-riding by definition
  • static because you can only hide a static
    method
  • Static means one per class, not one for each
    object no matter how many instance of a class
    might exist. This means that you can use them
    without creating an instance of a class.Static
    methods are implicitly final, because overriding
    is done based on the type of the object, and
    static methods are attached to a class, not an
    object. A static method in a superclass can be
    shadowed by another static method in a subclass,
    as long as the original method was not declared
    final. However, you can't override a static
    method with a nonstatic method. In other words,
    you can't change a static method into an instance
    method in a subclass.

17
Interfaces
  • An Interface consists of constants and abstract
    methods only
  • Syntax
  • interface interfaceName
  • constant declarations
  • abstract method declarations

18
Interfaces (continued)
  • Interface
  • Methods must be public and abstract
  • Constants must be public and final
  • No need to actually use these modifiers in method
    and constant definitions since theyre understood
    for an interface!

19
Implementing an interface
  • Implementing an interface syntax create a new,
    derived class, as per the following
  • class className implements interfaceName
    override defns of all abstract methods

20
Polymorphism
  • Permits the same method name to invoke
  • One response in objects of a base class
  • Another response in objects of a derived class
  • The determination of which overloaded method is
    actually called is made at run time
  • Known as run-time binding
  • Object being operated on ultimately determines
    the appropriate method to be called

21
Overriding or Overloading Polymorphism?
  • Polymorphism translates from Greek as many forms
    ( poly many, morph - forms)
  • Method overloading is the primary way
    polymorphism is implemented in Java
  • Overloaded methods appear in a subclass with same
    name but different parameter lists and return
    types
  • Late-binding (or run-time binding) also supports
    overriding
  • Overriding allows a subclass to re-define a
    method it inherits from it's superclass
  • An over-ridden method in a subclass has the same
    signature (name and parameter list) and also the
    same return type (different from C!)

22
Reference Variables as Class Members
  • Be aware of object assignment rules using the
    operator
  • Suppose two classes contain a String data member
  • To copy value of data from one class to another
    class
  • Use StringBuffer or perform a deep copy

23
(No Transcript)
24
(No Transcript)
25
Summary
  • For each class
  • Only one copy of each member method is created
  • Each object uses the same methods!
  • Location of the objects data members is provided
    to the member method by using a reference
    argument named this
  • Shallow copy occurs when two reference variables
    both locate one and the same object
  • Deep copy occurs when two reference variables
    locate two distinct objects, where both objects
    contain the same information
  • Inheritance
  • Capability of deriving one class from another
  • Polymorphism
  • Permits the same method to invoke different
    responses based on the object making the method
    call

26
(No Transcript)
27
Additional Applications
  • Complete simulation
  • Using two separate classes
  • Gas pump and Customer

28
Application 1 A Single-Class Gas Pump Simulation
  • Involves two distinct object types
  • Person and Gas pump
  • Pseudocode for top-level tasks
  • Put the pump in service
  • Display values
  • Pump an amount of gas

29
Application 1 A Single-Class Gas Pump Simulation
(continued)
  • Attributes of interest for pump
  • Amount of gallons in the supply tank and Price
    per gallon
  • Required operations include
  • Supplying the initial values for the pumps
    attributes
  • Interrogating the pump for attribute values
  • Satisfying the request for gas

30
Application 2 A Multiclass Gas Pump Simulation
  • Customer class
  • No attributes are needed
  • Operation arrival() provides a random arrival
    time between 1 and 15 minutes
  • Operation gallons() provides a random request of
    between 3 and 20 gallons of gas

31
Application 2 A Multiclass Gas Pump Simulation
(continued)
  • Each interaction between Customer and Pump
  • Obtain Customer arrival time
  • Obtain Customer request for gas
  • Activate Pump with request
Write a Comment
User Comments (0)
About PowerShow.com