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; }

catch Java Keyword

The catch keyword is used to define exception handling blocks in try−catch or try−catch−finally statements. Examples try { <block that may throw exceptions> } catch (<java.lang.Exception or subclass> e) { <code to handle exception e> } try { <block that may throw different exceptions> } catch (FooException e) { <code to handle FooException e> } catch (BarException e) { <code to handle BarException e> } try { <block that may throw exceptions> } catch (<java.lang.Exception or subclass> e) { <code to handle exception e> } finally { <statements that execute with or without exception> }

Java Language Keywords

Here is a list of keywords in the Java programming language. You cannot use any of the following as identifiers in your programs. The keywords const and goto are reserved, even though they are not currently used. true, false, and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs. The abstract Keyword  The boolean Keyword  The break Keyword  The byte Keyword   The case Keyword  The catch Keyword   The char Keyword  The class Keyword   The continue Keyword   The default Keyword  The do Keyword  The double Keyword  The else Keyword  The extends Keyword   The false Keyword  The final Keyword  The finally Keyword The float Keyword  The for Keyword   The if Keyword  The implements Keyword The import Keyword  The instanceof Keyword   The int Keyword  The interface Keywo...