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?
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
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();
}