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

case Java Keyword

The case is used to label each branch in a switch statement. Examples int arg = <some value>; switch (arg) { case 1: <statements> break; case 2: <statements> break; default: <statements> break; }

Java Program Structure

The Class Concept

A class is a collection of objects (or values) and a corresponding set of methods.  A class encapsulates the data representation and makes data access possible at a higher level of abstraction. Example 1: A set of vehicles with operations for starting, stopping, driving, get km/liter, etc. Example 2: A time interval, start time, end time, duration, overlapping intervals, etc. Example 3: A string, upper case, compare, lower case, etc.  str.equals(otherStr) – class/Java style   strcmp(str, otherStr) – C style