View
5
Download
0
Category
Preview:
DESCRIPTION
he
Citation preview
CIS 304Inheritance and Polymorphism
Dr. Steven Curl
Professor, Computer Information Systems
Cal Poly Pomona
Today’s Topics• Polymorphism
• Casting objects
• Comparing objects
What is Polymorphism?
Polymorphism
One of the most important features of object‐oriented programming. Using inheritance, polymorphism allows objects of different types to be treated as if they were the same by referring to a common parent class.
Notes:
1. Allows writing generic code that can then be applied to a range of different objects within the inheritance hierarchy.
2. Provides a common interface for working with subclasses of objects.
3. Decision as to which object to call is determined at run‐time increasing flexibility.
4. Because of delayed decision, polymorphism uses late binding.
Early vs. Late Binding
Early Binding
• Decision as to which object to call is determined at compile time.
• Speeds execution.
• Easier to debug since object is known at compile time.
• But less flexible.
Example:
ShoppingCart cart = new ShoppingCart( ); // Cart is created
cart.add(selectedItem ); // No doubt about cart
Early vs. Late Binding
Late Binding
• Decision as to which object to call is determined at run time.
• Slightly delayed execution
• More abstract
• Increased flexibility
Example:
Assume that Book and Video are subclasses of Product.
Product p;
p = collection.get(“PL”); // Pirate Latitudes is a book
// variable p is assigned at run time
System.out.println(p); // uses toString( ) method of Book
Early vs. Late Binding
Other examples of late binding
• Printing a document that doesn’t exist until run‐time when the user creates it.
• Saving a spreadsheet that didn’t exist until run time.
• Allowing a varying number of graphical objects to be created at run time and still have them respond to mouse events (e.g., mine sweeper).
• A playlist with any number of songs that all respond to the play( ) message.
• An application that allows the user to open new documents and shows these in multiple windows.
• A utility that permits varying numbers of file transfers that didn’t exist until the user created the sessions.
Casting Objects with Inheritance
Two types of casting:
• implicit Automatically done by Java
• explicit( ) Requires new type to be specified in code
Rule for casting with inheritance:
Java will implicitly cast a subclass to its parent class (upcast) but requires explicit cast from a parent to its child (downcast).
Example:
Product p;p = new Book( ); // Implicit cast is okay since Book is a childBook b = p; // Not okay, product may not be a BookBook b = (Book) p; // Explicit cast required
Casting Objects with Inheritance
Casting affects which methods are available:
When casting from a child to its parent class, Java will only allow access to data and methods that are defined by the parent class
Example:
Product p;
p = new Book( ); // Implicit cast is okay since Book is a childp.setAuthor(“Murach”); // Not okay, product may not be a Book
Book b = (Book) p;
b.setAuthor(“Murach”); // Correct but be sure that product really is a book
Comparing Objects
Using .equals( )
• Compares whether two variables refer to the same object
• Does NOT compare if two variables hold the same data
Example 1:
Product product1 = new Product( );
Product product2 = product1;
if (product1.equals(product2)) // returns true
Example 2:
Product product1 = new Product( );
Product product2 = new Product( );
if (product1.equals(product2)) // returns false
Questions
Try the review exercises
in Blackboard.
Recommended