The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
May 2000



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.



Posted by Matt Gerrans on September 26, 2001 at 2:47 PM

No overloading is just a handy feature, it is not polymorphism.

Overriding is what allows for polymorphism; this allows a base class or interface reference to behave differently at runtime depending on what derived object it actually refers to. Here's a simple example of the same reference (spot) behaving differently depending upon which object it is referring to:

interface Animal
   String initiateCommunicationsProtocol();

class Dog implements Animal
   public String initiateCommunicationsProtocol() { return "Woof!"; }

class Cat implements Animal
   public String initiateCommunicationsProtocol() { return "Meow!"; }

public class PetsAgog
   public static void main(String[] args)
      Animal spot = new Dog();
      System.out.println( "spot says " + spot.initiateCommunicationsProtocol() );
      spot = new Cat();
      System.out.println( "Now spot says " + spot.initiateCommunicationsProtocol() );


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us