Polymorphism - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Polymorphism

Description:

A postcodition is the state of the program after the method is run. ... Overriding involves writing a method in a subclass with the same name and same ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 15
Provided by: rob1125
Category:

less

Transcript and Presenter's Notes

Title: Polymorphism


1
Polymorphism
  • Robert Salkin
  • CSI 205 OOP using Java
  • Summer 2005, 6W1

2
Odds and Ends
  • Primitive boolean
  • Ternary Operator
  • Other Prints
  • String Manipulation
  • References as Return Types
  • Coding Strategies
  • Precondition/Postcondition

3
Primitive boolean
  • In addition to primitives that can hold numerical
    values, there is a boolean datatype to hold truth
    values.
  • Can only hold true or false.
  • boolean doneYetfalse
  • while(!doneYet)
  • //do something until
  • doneYettrue

4
Ternary Operator
  • The ternary operator is a condensed if/else,
    usually used to select one of two values.
  • (condition)?(true_value)(false_value)
  • int alwaysNonNegativeX(xlt0)?(0)(x)
  • String x(doneYet)?(true)(false)

5
Other Prints
  • These are (or should be) going out of style, but
    will remain a part of Java forever.
  • System.out.print(hi\n)
  • Prints a message like printf, but cannot take
    format specifiers.
  • System.out.println(hi)
  • Prints a message like print, but always inserts
    an extra newline at the end of the print
    operation.

6
String Manipulation
  • Strings can be concatenated together using the
    plus sign ().
  • When two strings are pasted together, they are
    said to be concatenated together.
  • The first String is placed before the second
    String, and so on.
  • String shello world!\n
  • Primitives and objects may be added to strings,
    as long as the string comes first toString() is
    automatically run on the object.
  • int x42
  • String sThe number is x

7
References as Return Types
  • Object references can be used as return types for
    methods just like primitives.
  • When a primitive is returned, the value of the
    primitive is returned.
  • When an object reference is returned, a reference
    to the object is returned not a copy of the
    object.

8
Coding Strategies
  • When coding, break the specifications up into
    data, blocks, methods, or other logical
    components.
  • Compile often to see if youre on the right track
    do NOT wait until the end to compile, only to
    find out you wrote code that doesnt do anything
    close to what you wanted it to.
  • Do not assume code does something for you when it
    really doesnt.
  • Code is dumb, the coder is smart the code
    doesnt know when the coder intends to do
    something not explicitly written.
  • Example

9
Precondition/Postcondition
  • A precondition is a specific state the program
    must be in in order for the method to run
    appropriately.
  • A postcodition is the state of the program after
    the method is run.
  • These descriptions need only cover the
    non-trivial, related state concerns.

10
Polymorphism
  • Polymorphism allows the same code to be run with
    different, more specific objects.
  • Implementations may be more abstract and general,
    relying on polymorphism to make the right action
    happen.
  • Polymorphism may be used with object references
    as well as methods.

11
Polymorphic References
  • An object may be referenced by a more general
    reference variable.
  • Car myCarnew Car()
  • BMW myBMWnew BMW()
  • myCarmyBMW //ok.
  • myBMWmyCar //NOT ok.

12
Polymorphic Methods
  • Polymorphism allows for methods to be written
    that are specific to a particular subclass.
  • Animal anew Frog()
  • a.move() //a quick hop.
  • anew Turtle()
  • a.move() //a slow walk.

13
Overloading vs. Overriding
  • Overloading a method means that methods of the
    same name have been defined with different
    arguments.
  • Overriding involves writing a method in a
    subclass with the same name and same arguments in
    order to mask the functionality of the
    superclasss method in favor of the new method.

14
Abstract Class
  • An abstract class is unfinished.
  • An Animal may require that a move() method be
    defined.
  • Animal is too general without a subclass filling
    in the blanks, like move().
  • The abstract keyword is used on the class as well
    as the unfinished methods.
  • Example
Write a Comment
User Comments (0)
About PowerShow.com