Polymorphism via Interfaces - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Polymorphism via Interfaces

Description:

Polymorphism from Wepodia http://www.webopedia.com/TERM/p/polymorphism.html. In general, polymorphism is the ability to appear in many forms ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 14
Provided by: rickmercer
Category:

less

Transcript and Presenter's Notes

Title: Polymorphism via Interfaces


1
Polymorphism via Interfaces
  • By Rick Mercer with help from many sources

2
Polymorphism from Wepodia http//www.webopedia.com
/TERM/p/polymorphism.html
  • In general, polymorphism is the ability to appear
    in many forms
  • In object-oriented programming, polymorphism
    refers to a programming language's ability to
    process objects differently depending on their
    data type or class
  • Polymorphism is considered to be a requirement of
    any true object-oriented programming language
    (OOPL)

3
Polymorphism from mercer
  • To understand polymorphism, take an example of a
    typical workday in an office. Kim brought in
    pastries and everyone stood around chatting. When
    the food was mostly devoured, Jim, the president
    of the company, invited everyone to Get back to
    work. Sue went back to read a new section of a
    book she was editing. Tom continued laying out a
    book. Stephanie went back to figure out some
    setting in her word-processing program. Ian
    finished the company catalog.

4
Polymorphism
  • Jeni met with Jim to discuss a new project. Chris
    began contacting professors to review a new
    manuscript. And Krista continued her Web search
    on whether colleges are using C or Java. Marty
    went back to work on the index of his new book.
    And Kim cleaned up the pastries.

5
Polymorphic Messages
  • 9 different behaviors with the same message!
  • The message Get back to work is a polymorphic
    message
  • a message that is understood by many different
    types of object (or employees in this case)
  • but responded to with different behaviors based
    on the type of the employee Editor, Production,
    Marketing,

6
Polymorphism
  • Polymorphism allows the same message to be sent
    to different types to get different behavior
  • In Java, polymorphism is possible through
  • inheritance
  • Overide toString to return different values
  • interfaces
  • Collections.sort sends compareTo messages to
    elements that understand Comparable

7
Polymorphism
  • The runtime message finds the correct method
  • same message can invoke different methods
  • the reference variable knows the type
  • aString.compareTo(anotherString)
  • anInteger.compareTo(anotherInteger)
  • aButton.actionPerformed(anEvent)
  • aTextField.actionPerformed(anEvent)
  • aHashSet.add(anObject)
  • aTreeSet.add(anObject)

8
Polymorphism through Interfaces
  • An interface describes a set of methods
  • NOT allowed constructors, instance variables
  • Interfaces are implemented by classes
  • 646 java classes implement 1 or more interfaces
  • Multiple classes implement the same interface
  • are then guaranteed to have the same methods

9
Implementing Comparable
  • You can have types implement Comparable
  • Need to do this to be sorted by Collections.sort
  • public interface Comparable
  • / Return 0 if two objects are equal less
    than
  • zero if this object is smaller greater
    than
  • zero if this object is larger. /
  • public int compareTo(Object rhs)
  • Try to sort BankAccounts without Comparable
  • Implement Comparable and sort
  • Team Activity Change compareTo to sort by balance

10
Let BankAccount also be a Comparable
  • public class BankAccount implements Comparable
  • private String my_ID
  • private double my_balance
  • public BankAccount(String ID... // stuff
    deleted
  • public int compareTo(Object argument)
  • // Must complete this method or else it's an
    error
  • // You will always see cast from Object to
    this class
  • BankAccount right (BankAccount)argument
  • return my_ID.compareTo(right.getID())

promise that this class has int
compareTo(Object)
Add compareTo method
11
Displaying an Image
  • Use JOptionPane to display message
  • JOptionPane.showMessageDialog(null,
  • "Hello, World!")
  • Note icon to the left

12
ImageIcon implements Icon
  • JOptionPane.showMessageDialog(null, "Hello,
    World!", "Message",
  • JOptionPane.INFORMATION_MESSAGE, new
    ImageIcon("home34.jpg"))

13
Here is the method heading
  • public static void showMessageDialog(Component par
    entComponent,
  • Object message,
  • String title,
  • int messageType,
  • Icon icon)
  • The 5th parameter is Icon, an interface
  • The argument must be an instance of a class that
    implements the Icon interface
  • Such as ImageIcon
Write a Comment
User Comments (0)
About PowerShow.com