You don't have to have an abstract class, but yes that's one kind of polymorphism. Another kind is methods in the same class with the same name and the same semantics, but different signatures.
polymorphism can be acheived two ways
overloading - static binding/early binding
overriding - dynamic binding/late binding
OVERLOADING
in case of overloading, you will have same method names with different signatures within a class.
display(char arr[])
display(char arr[], int sub)
display(char arr[], char subarr[])
display(char arr[],char subarr[],int sub)
NOTE: return type is not considered. only the method parameters should be different. here, the method to be called is decided at the compile time itself, based on the signature.
OVERRIDING
in case of overriding, a simple example would be of inheritance. in case of inheritance, you have a baseclass/interface and subclasses. here, the method signature (which also can have a method body), exists in base class and same method (without modifying the signature) exists in the subclass also.
interface figure -> draw()
class triangle implements figure -> draw()
class polygon implements figure -> draw()
above you can see an interface which has a method draw() and same has been implemented in triangle and polygon (you can have a base class and do extends also). since the reference of a derived class object can be assigned to a base class reference (OOP concept), you can create objects of triangle and polygon and assign it to the interface reference.
1. figure f; //create an interface reference
2. triangle t = new triangle(); // create a new triangle
3. polygon p = new polygon(); // create a new polygon
4. f = t; // assign triangle object reference to interface reference
5. f.draw(); // call draw -> which draw() do you think it will call?
6. f=p; // assign polygon object reference to interface reference
7. f.draw(); // call draw -> which draw() do you think it will call?
now, see the lines 5 and 7. are'nt they same? but, the methods called would be different. in case of line 5, draw() of triangle would be called because at that stage, f holds the reference of t. in case of line 7, draw() of polygon would be called because at that stage, f holds the reference of p.
so, the crux here is, the method to be called is decided at run time (based on which reference is assigned) and NOT at compile time. This is runtime polymorphism.
hope this is enough.
you can write to me at bparun@indiatimes.com
semantics: "The meaning or the interpretation of a word, sentence, or other language form." In other words, the two (or more) methods mean the same thing. If I have two methods called fetchData with different signatures, and one retrieves the data for an object from a database and the other from a file, that's polymorphism because the methods mean the same thing, but the implementation is different. If one of the methods retrieves data, and the other one displays an image of the android Data from Star Trek, that's not polymorphism, it's just two methods. This is because polymorphism means many forms, and in that case the methods are not the same thing in different forms, they're two completely different things.