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 classesPolymorphism
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
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.
Very well done, good work on everything
ReplyDeletekeep it up man..
ReplyDeletekeep it up man..
ReplyDelete