participate


New To Java Technology Archive - polymorphism concept ?
<<   Back to Forum  |   Give us Feedback
This topic has 9 replies on 1 page.
pwing
Posts:172
Registered: 3/2/98
polymorphism concept ?   
Apr 11, 2003 12:03 PM

 
Hi,

polymorphism relates to dynamic binding, isn't it?

So, polymorphism means many forms, just like methods overloading. What is the difference between overloading and polymorphism ? Very confusing !

thanks.

P
 
tjacobs01
Posts:10,225
Registered: 10/11/01
Re: polymorphism concept ?   
Apr 11, 2003 12:17 PM (reply 1 of 9)  (In reply to original post )

 
overloading is not polymorphism. Overriding is polymorphism.

Polymorphism is an abstract process of relating types(classes) of things by what they have in common.

ie:
Are you a currency? Yes
Are you a dollar? Yes
What are you worth with respect to dollars? 1

Are you a currency? Yes
Are you a dollar? No
What are you worth with respect to dollars? 1.6
 
Nikita04
Posts:406
Registered: 4/1/02
Re: polymorphism concept ?   
Apr 11, 2003 1:10 PM (reply 2 of 9)  (In reply to #1 )

 

Some books will say that overloading is a type of polymorphism. And that is true, but that's not to be confused with dynamic binding.
 
pwing
Posts:172
Registered: 3/2/98
Re: polymorphism concept ?   
Apr 11, 2003 8:46 PM (reply 3 of 9)  (In reply to #2 )

 
an abstract process of different classes with common methods...

So, we have different classes names under the same abstract class generalization which each class has the same method signatures, is that correct ?

thanks.

P
 
nasch
Posts:2,522
Registered: 3/6/01
Re: polymorphism concept ?   
Apr 11, 2003 9:54 PM (reply 4 of 9)  (In reply to #3 )

 
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.
 
pwing
Posts:172
Registered: 3/2/98
Re: polymorphism concept ?   
Apr 11, 2003 10:33 PM (reply 5 of 9)  (In reply to #4 )

 
Thanks, but what do you mean by same semantics ?
 
scoit
Posts:14
Registered: 2/2/03
Re: polymorphism concept ?   
Apr 11, 2003 10:47 PM (reply 6 of 9)  (In reply to #4 )

 
thanks
 
bparun
Posts:58
Registered: 2/5/00
Re: polymorphism concept ?   
Apr 11, 2003 11:46 PM (reply 7 of 9)  (In reply to original post )

 
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

bye.
arun.
 
nasch
Posts:2,522
Registered: 3/6/01
Re: polymorphism concept ?   
Apr 12, 2003 9:47 PM (reply 8 of 9)  (In reply to #5 )

 
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.
 
uj
Posts:1,371
Registered: 6/2/98
Re: polymorphism concept ?   
Apr 12, 2003 10:22 PM (reply 9 of 9)  (In reply to #7 )

 
polymorphism can be acheived two ways
overloading - static binding/early binding
overriding - dynamic binding/late binding

And in addition, the most importent mechanism for polymorphism - class extension.
 
This topic has 9 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 22
  • Guests : 132

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums