polymorphism

I am learning java 2 and OOP in general and the world polymorphism keeps coming up. i believe method overloading has something to do with it and i understand that but seems like there is more to it. can anyone enlighten me? by the way, i cant post any messages using IE , Opera, or Netscape 4.7. i had to use Netscape 6.1. the status bar on the other browsers just kept on flashing text and eventually just quit. anyone else have that prob?

[454 byte] By [bunchofpixels] at [2007-9-26 4:40:44]
# 1
world=word
bunchofpixels at 2007-6-29 18:02:32 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2

polymorphism = method in sub class "hides" a method in super class

pseudo java:

class animal {

method speak() {}

}

class fish extends animal {

method speak() {

print "[blop] [blop] [blop]"

}

}

class shark extends fish {

method speak() {

print "jaws"

}

}

animal myFish = new fishk()

animal myShark = new shark()

myFish.speak() // prints "[blop] [blop] [blop]"

myShark.speak() // prints "jaws"

(also called overriding)

overloading = methods have the same names but different formal parameter lists (and thus possibly different behaviour)

Take eg. the method System.out.print() - it has an overload for every possible type. http://java.sun.com/j2se/1.4/docs/api/java/io/PrintStream.html

jsalonen at 2007-6-29 18:02:32 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3
would it be possible to access animal's speak method when using a shark object?
bunchofpixels at 2007-6-29 18:02:33 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4
yup. just call super.speak() from Shark.
eriklindquist at 2007-6-29 18:02:33 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 5
have to call it from within the shark class? i tried myShark.super.speak() and it didnt work. any way to access the overridden function outside of the class itself?
bunchofpixels at 2007-6-29 18:02:33 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 6
> any way to access the overridden function outside of> the class itself?No. Why would you want to?
nerd2004 at 2007-6-29 18:02:33 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 7

Didn't understand what your question was bunch. I was thinking more like this:

public class Shark extends Animal {

public void speak() {

super.speak();

}

}

If you wanted to call the Animal's version of speak from outside Shark then just instantiate an Animal object instead. or, add a method to Shark that explicitly calls it's super:

Shark...

public void speak() {

System.out.println("Im a shark");

}

public void animalSpeak() {

super.speak();

}

eriklindquist at 2007-6-29 18:02:33 > top of Java-index,Archived Forums,New To Java Technology Archive...