eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Resources
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak

 

introduction to Perl 5 for web developers
Polymorphism  
Okay, there is one final feature of objects that you need to understand.

Objects are smart enough to be dynamic. Specifically, they have the ability to react differently depending on the situation.

This is important because it makes the API dependable and easy to understand for the incoming developer.

Let's look at an example. Suppose you have a method called "print()".

The first question you might ask is, "what does this method print: pictures, text, or what?"

Well using "polymorphism", an object can be made to handle any scenario with the exact same method name. Thus, depending on what the object is asked to print, it will be able to print it.

Instead of having separate methods like printPicture(), printLetters(), and printNumbers() you have one unified print() and the object itself determines how to handle the different types of situations.

One reader, Dave wrote in and said, "Your description of Polymorphism is wrong. I will attempt to clarify. Please excuse the nature of my opening sentence. I hoped to get your attention so I can help (not criticize) to avoid misleading your readers regarding your Java tutorial.

In a nutshell, polymorphism is bottom-up method calling. Simply put, using your Animal/Mammal/Cat example:

void test()
{
 Cat simon = new Cat();
 Animal creature = simon;  // safe upcasting
 creature.eat(); // polymorph = Cat.eat()
 Dog rover = new Dog();

  // rover safely upcast to Animal reference
 feed(rover);
 feed(simon);
}
void feed(Animal a )
{
  // creature.eat() = Cat.eat()
  // feed(rover)    = Dog.eat()
  // feed(simon)    = Cat.eat()
  a.eat();
}

Here, it's the Animal.eat() which is polymorphic.

Polymorphism: "Calling a Java/virtual method using a reference to a more generalized superclass of a real object invokes the method in the actual object (the more specific subclass) using a bottom-up searching mechanism".

Your Java Tutorial's definition of polymorphism states that function name overloading is the same thing, which is an incorrect statement."

I have to say that Dave is correct, however, I want to focus on overloading here, so I hope to make both points by adding this note!

Previous | Next | Table of Contents