CS110 Lecture 20 May 4, 2005 - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CS110 Lecture 20 May 4, 2005

Description:

Holiday day; day = new Christmas(); Holiday. Christmas. 14. References and Inheritance ... has a method called celebrate, and the Christmas class overrides it. ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 20
Provided by: JCr52
Learn more at: http://www.cs.umb.edu
Category:
Tags: cs110 | lecture

less

Transcript and Presenter's Notes

Title: CS110 Lecture 20 May 4, 2005


1
CS110- Lecture 20May 4, 2005
  • Agenda
  • Wrapper Classes
  • Autoboxing and Unboxing
  • Binding
  • Polymorphism
  • Polymorphism via inheritance
  • Polymorphism via interfaces

2
Wrapper Classes
  • The java.lang package contains wrapper classes
    that correspond to each primitive type

3
Wrapper Classes
  • The following declaration creates an Integer
    object which represents the integer 40 as an
    object
  • Integer age new Integer(40)
  • An object of a wrapper class can be used in any
    situation where a primitive value will not suffice

4
Wrapper Classes
  • For example, some objects serve as containers of
    other objects
  • Primitive values could not be stored in such
    containers, but wrapper objects could be
  • Wrapper classes also contain static methods that
    help manage the associated type

5
Wrapper Classes
  • For example, the Integer class contains a method
    to convert an integer stored in a String to an
    int value
  • int num Integer.parseInt(str)
  • The wrapper classes often contain useful
    constants as well
  • For example, the Integer class contains MIN_VALUE
    and MAX_VALUE which hold the smallest and largest
    int values

6
Autoboxing
  • Autoboxing is the automatic conversion of a
    primitive value to a corresponding wrapper
    object
  • Integer obj
  • int num 42
  • obj int
  • The assignment creates the appropriate Integer
    object

7
Unboxing
  • The reverse conversion (called unboxing) also
    occurs automatically as needed

8
Binding
  • Consider the following method invocation
  • obj.doIt()
  • At some point, this invocation is bound to the
    definition of the method that it invokes
  • If this binding occurred at compile time, then
    that line of code would call the same method
    every time
  • However, Java defers method binding until run
    time -- this is called dynamic binding or late
    binding
  • Late binding provides flexibility in program
    design

9
Polymorphism
  • Polymorphism is an object-oriented concept that
    allows us to create versatile software designs.
  • The term polymorphism literally means "having
    many forms"
  • A polymorphic reference is a variable that can
    refer to different types of objects at different
    points in time

10
Polymorphism
  • The method invoked through a polymorphic
    reference can change from one invocation to the
    next
  • All object references in Java are potentially
    polymorphic

11
Polymorphism
  • Suppose we create the following reference
    variable
  • Occupation job
  • Java allows this reference to point to an
    Occupation object, or to any object of any
    compatible type
  • This compatibility can be established using
    inheritance or using interfaces

12
Polymorphism
  • Careful use of polymorphic references can lead to
    elegant, robust software designs

13
References and Inheritance
  • An object reference can refer to an object of its
    class, or to an object of any class related to it
    by inheritance

Holiday day day new Christmas()
14
References and Inheritance
  • Assigning a child object to a parent reference is
    considered to be a widening conversion, and can
    be performed by simple assignment
  • Assigning an parent object to a child reference
    can be done also, but it is considered a
    narrowing conversion and must be done with a cast

15
Polymorphism via Inheritance
  • It is the type of the object being referenced,
    not the reference type, that determines which
    method is invoked
  • Suppose the Holiday class has a method called
    celebrate, and the Christmas class overrides it.

16
Polymorphism via Inheritance
  • Now consider the following invocation
  • day.celebrate()
  • If day refers to a Holiday object, it invokes the
    Holiday version of celebrate if it refers to a
    Christmas object, it invokes the Christmas version

17
Polymorphism via Interfaces
  • An interface name can be used as the type of an
    object reference variable
  • Speaker current
  • The current reference can be used to point to any
    object of any class that implements the Speaker
    interface

18
Polymorphism via Interfaces
  • The version of speak that the following line
    invokes depends on the type of object that
    current is referencing
  • current.speak()
  • Suppose two classes, Philosopher and Dog, both
    implement the Speaker interface, providing
    distinct versions of the speak method

19
Polymorphism via Interfaces
  • In the following code, the first call to speak
    invokes one version and the second invokes
    another
  • Speaker guest new Philospher()
  • guest.speak()
  • guest new Dog()
  • guest.speak()
Write a Comment
User Comments (0)
About PowerShow.com