Forums Register Login

overriding and polymorphism

+Pie Number of slices to send: Send
I had a mock question that says only OVERLOADING is an example of polymorphism. but not overriding. is this true? why isn't overriding an example of polymorphism? please help!
thanks a lot!
chun
+Pie Number of slices to send: Send
"I had a mock question that says only OVERLOADING is an example of polymorphism. but not overriding. is this true? why isn't overriding an example of polymorphism? please help! "
No, overriding is for polymorphism, overloading is not.
LHS
+Pie Number of slices to send: Send
I think overloading and overriding are both polymorphism
see: http://www.ergoit.co.uk/ObjPoly/WhatWhy.html
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
Kevin,
Polymorphism gotta go with late binding. In "Thinking in Java",
edition 1, page 321, Bruce Eckel says:


"People are confused by other, non-object-oriented features of Java, like method overloading, which are sometimes presented as object-oriented. Don't be fooled: If it isn't late binding, it isn't polymorphism"


Kelvin, I'm coming from a C++ background, I don't know if polymorphism still remains the same concept in Java, but in C++, polymorphism must be obtained trough late binding, inheritance, virtual funcions, and pointers/references.
Please verify.
Thanks,
Tho
+Pie Number of slices to send: Send
1. overriding is hiding the super class implementation. There could be only one overriding method with the same signature as the superclass in the subclass.
2. whereas overloading is providing multiple forms of the method based on different arguement types. Java determines particular overloaded method to be executed during run time by late binding.
I would agree to the Mock exam question that overloading is polymorphism.
+Pie Number of slices to send: Send
Venkat,
"Java determines particular overloaded method to be executed during run time by late binding."
I don't think so. Overloadded method can be specified at compile time. The reason is that at compiler time the compiler already knows what method to invoke. (by the diff. in method signatures -- There's no need to have an object-oriented language to provide you this feature. Procedural language does support this feature.)
LHS


[This message has been edited by Hungson Le (edited February 01, 2001).]
+Pie Number of slices to send: Send
See, why overloading is called as an example of Polymorphism:
When overloaded method is invoked it dynamically determines the method according to the argument passed. Whereas in case of overriding if you want to call super class method you have to explicitly call super.x() otherwise it will take local method, so here the dynamism is not working, hence, it is not an example of polymorphism. Invariably you can have the same signature in both overloading and overriding.
Please correct me if I am wrong.
+Pie Number of slices to send: Send
hi!
I agreed with waht Vinket says. Overloading is an example of polymorphism. Overriding means we are we are overriding the method.
You can check it out in rhe oR KhalidMugahl book.They have given a good explanation
+Pie Number of slices to send: Send
I think both overloading and overriding are polymorphism.
For Overloading, Same as shabbir said.
For Overriding, See the below code.
<code>
class SuperClass {
void p() {
System.out.println("Super");
}
}
class SubClass extends SuperClass {
void p() {
System.out.println("Sub");
}
public static void main(String args[]) {
SuperClass obj1 = new SubClass();
obj1.p();
}
}
</code>
See? Late Binding for the method p.
+Pie Number of slices to send: Send
One more point.
overrinding happens only in inheritance.
But, overloading - within the class the same signature with different flavours.
+Pie Number of slices to send: Send
I have to agree with Hungson that only overriding is an example of polymorphism, because of dynamic/late binding.
There is no late binding in the case of overloaded methods because if a function call does not match any given method signature then the compiler will generate an error. So the compiler knows which method to call and the run-time has no decisions to make.
Method overloading can be demonstrated using procedural languages like C and Oracle PL/SQL.
+Pie Number of slices to send: Send
I am sorry I have to take back something I said.
I guess C cannot be used to demonstrate function overloading. But it is possible to overload functions in Oracle database packages.
+Pie Number of slices to send: Send
Folks,
We need some expert to confirm what we've been discussing on this topic. Even though I'm coming from a C++ backgourng, I'm positive that overloading is not late binding. Usually late binding takes some extra overhead that slow down your program. I don't see any reason why would the compiler ever come to late binding when early binding for overloadding can happen at compile time.???
Any bartender out there, I need your word just to confirm what I say here.
Thanks,
LHS
p.s.
Krishna, you were right. In C, overloading is possible:
int add(int a, int b);
int add(double a, double b);

[This message has been edited by Hungson Le (edited February 02, 2001).]
+Pie Number of slices to send: Send
hi! every one !
hope my xplanation will help you the difference btw overloading and overriding !
" polymorphism " is made of two words "Poly" and " Morphic"
" poly " == "many"
" morphic" == "form"
so
if anything exists in many form that phenomenon is called polymorphism. e.g : carbon , graphite , diamond etc.
now coming to java :
what happens in overloading : Tthe same name with different arguments . so the method is made to exist in many form !
but in case of overriding the method does not exist in different forms although it exists in different classes( derived)
but the form ( argument list ) doesn't change !
thats why it doesn't support polymorphism and it supports only inheretance .
another example of polymorphism is operator overloading which is not in java ( only + is overloaded for Strings).
if anyone disaggrees plz feel free to correct me.
anurag
+Pie Number of slices to send: Send
Overloading - Parametric polymorphism
Overriding - Runtime polymorphism


------------------
Velmurugan Periasamy
Sun Certified Java Programmer
----------------------
Study notes for Sun Java Certification
http://www.geocities.com/velmurugan_p/
+Pie Number of slices to send: Send
Overloading is Weak/Static Polymorphism
Compiler treats overloaded methods as different methods & knows which method to call. This is called static binding.
Overidding is True/Runtime Polymorphism
Compiler cannot determine which method to call until runtime.
Either super class/sub class method will be called depending on which method is applied to the object. Since this is resolved only at runtime , it is called late binding or dynamic method dispatch.
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2280 times.
Similar Threads
Polymorphism
polymorphism
Method Overloading and overriding
Polymorphism
Simple Question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:32:59.