Dynamic Binding and Polymorphism

   

Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 7.  Inheritance


Object-orientation is defined by three key concepts: encapsulation, inheritance, and polymorphism. The previous chapter dealt with encapsulation, and thus far in this chapter we have been talking about inheritance, but the final key concept is polymorphism. The term literally means many shapes and refers to one object being used in multiple differing ways.

Consider a car for a moment. If you have a driver's license, do you need any knowledge specific to a specialization of a car to drive it? If you know how to drive a Porsche, can you still drive a Pinto? Would you want to?

The last question might be no, but the real answer is that if you know how to drive a Porsche, you should have no problem at least understanding how to drive a Pinto. If you write a new class Driver, how would you ask it to drive one of your cars? Listing 7.8 shows the code for a potential driver.

Listing 7.8 Driver1.java
 public class Driver1  {      public void drivePorsche( Porsche2 p )      {          System.out.println( "Driving: " + p );           p.start();          for( int i=0; i<10; i++ )          {              p.accelerate();              System.out.println( "Current speed: " + p.getCurrentSpeed() );          }          for( int i=0; i<5; i++ )          {              p.decelerate();              System.out.println( "Current speed: " + p.getCurrentSpeed() );          }          p.stop();      }      public void drivePinto( Pinto2 p )      {          System.out.println( "Driving: " + p );          p.start();          for( int i=0; i<10; i++ )          {              p.accelerate();              p.rollStart();              System.out.println( "Current speed: " + p.getCurrentSpeed() );           }          for( int i=0; i<5; i++ )          {              p.decelerate();              System.out.println( "Current speed: " + p.getCurrentSpeed() );           }          p.stop();      }      public static void main( String[] args )      {          Driver1 d = new Driver1();          Porsche2 porsche = new Porsche2();          d.drivePorsche( porsche );          Pinto2 pinto = new Pinto2();          d.drivePinto( pinto );       }  } 

The Driver1 class defines two methods that drive cars: drivePorsche and drivePinto. If you look closely, the methods are the same except they accept different types of cars, a Porsche and a Pinto, respectively.

If we apply the earlier principal that a driver really knows how to drive any type of car to object-oriented programming, we can leverage the fact that both Pinto and Porsche share a common super class. We can define a Driver class method that knows how to drive a Car, and then pass instances of the Porsche2 and Pinto2 to it. Thus, the Porsche2 and Pinto2 classes can be used in different ways: as a Car and as their specialized classes. Thus, defines the term polymorphism.

The limitation of using a Porsche2 as a Car is that you can only access the methods defined in the Car class. You cannot access Porsche2 specific methods such as engageTurbos and engageNOS.

Listing 7.9 shows this concept in action.

Listing 7.9 Driver2.java
 public class Driver2  {      public void drive( Car c )      {          System.out.println( "Driving: " + c );          c.start();          for( int i=0; i<10; i++ )          {              c.accelerate();              System.out.println( "Current speed: " + c.getCurrentSpeed() );          }          for( int i=0; i<5; i++ )          {              c.decelerate();              System.out.println( "Current speed: " + c.getCurrentSpeed() );          }           c.stop();      }      public static void main( String[] args )      {          Driver2 d = new Driver2();          Porsche2 porsche = new Porsche2();          d.drive( porsche );          Pinto2 pinto = new Pinto2();          d.drive( pinto );      }  } 

The Driver2 class defines a single drive method that, because of polymorphism, can accept either a Porsche2 or a Pinto2 instance. When there are two implementations of a method, for example the Porsche2 class overrides the Car's accelerate method, how does the compiler know to use the Porsche2's instance instead of the Car's instance? It is, after all, using the Porsche2 instance as a Car.

The answer to this a term that goes hand in hand with polymorphism called dynamic binding. Dynamic binding means that the Java Runtime Engine will resolve the implementation at runtime, not at compile time (when the Driver2 class is being compiled). This means that the Driver2 class will know how to drive any new car that you define in the future.


       
    Top
     



    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net