Skip to main content

Polymorphism

Polymorphism

Enables “programming in the general”The same invocation can produce “many forms” of results

Interfaces

Implemented by classes to assign common functionality to possibly unrelated classes 

Polymorphism
When a program invokes a method through a superclass variable, the correct subclass version of the method is called, based on the type of the reference stored in the superclass variable
The same method name and signature can cause different actions to occur, 
depending on the type of object on which the method is invoked Polymorphism enables programmers to deal in generalities and let the execution-time environment handle the specifics. Programmers can command objects to behave in manners appropriate to those objects, without knowing the types of the objects (as long as the objects belong to the same inheritance hierarchy). 
  • Promotes extensibility
  • New objects types can respond to existing method calls Can be incorporated into a system without modifying base system
  • Only client code that instantiates the new objects must be modified To accommodate new types

Polymorphism Promotes Extensibility

  • Software that invokes polymorphic behavior independent of the object types to which messages are sent. 
  • New object types that can respond to existing method calls can be incorporated into a system without requiring modification of the base system. Only client code that instantiates new objects must be modified to accommodate new types.

Demonstrating Polymorphic Behavior 

  • A superclass reference can be aimed at a subclass objecta subclass object “is-a” superclass object the type of the actual referenced object, not the type of the reference, determines which method is called
  • A subclass reference can be aimed at a superclass object only if the object is downcasted
Here is the updated class:


Next, create the RoadBike class. Because road or racing bikes have skinny tires, add an attribute to track the tire width. Here is the RoadBike class:


Here is a test program that creates three Bicycle variables. Each variable is assigned to one of the three bicycle classes. Each variable is then printed.



Comments

Post a Comment

Popular posts from this blog

Polymorphism and Dynamic Binding

Polymorphism: One piece of code works with all shape objects. Dynamic binding: How polymorphism is implemented Take previous Shape class hierarchy  remove inheritance  remove general and abstract class Shape 

Generalization and Specialization

Generalization creates a concept with a broader scope. Specialization creates a concept with a narrower scope. Reusing the interface!  Inheritance: get the interface from the general class. Objects related by inheritance are all of the same type.

History of Java

1990 Oak (interactive television, big failure) 1994 Java (for the Internet) Main feature: "Write Once, Run Any Where" => wrap the operating system so they all look the same Designed for A fresh start (no backward compatibility) “Pure” OOP: C++ Syntax, Smalltalk style Improvements over C++ much harder to write a bad program  Internet programming Very hard to create a virus  Run in a web browser (and at the server) There is a speed issue (from Java 1.3 and up much better) C# Microsoft's “Java-Killer” project release 2001 Language very similar to Java   Commen-Language Runtime (CLR) supports 30+ languages