File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding and polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding and polymorphism" Watch "overriding and polymorphism" New topic
Author

overriding and polymorphism

Chun Wang
Ranch Hand

Joined: Jan 24, 2001
Posts: 42
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
Hungson Le
Ranch Hand

Joined: Jan 25, 2001
Posts: 32
"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


Hungson Le<BR>
Kevin Hou
Greenhorn

Joined: Jan 18, 2001
Posts: 27
I think overloading and overriding are both polymorphism
see: http://www.ergoit.co.uk/ObjPoly/WhatWhy.html
Kevin Hou
Greenhorn

Joined: Jan 18, 2001
Posts: 27
another good website for POLYMORPHISM
http://www.fsu.umd.edu/dept/cosc/htracy/cosc390/LM7Polymorphism/PolymJava.html
Hungson Le
Ranch Hand

Joined: Jan 25, 2001
Posts: 32
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
venkat_alladi
Greenhorn

Joined: Jan 31, 2001
Posts: 12
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.
Hungson Le
Ranch Hand

Joined: Jan 25, 2001
Posts: 32
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).]
V Srinivasan
Ranch Hand

Joined: Aug 16, 2000
Posts: 99
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.
shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
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
Wong KK
Ranch Hand

Joined: Jan 15, 2001
Posts: 52
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.
V Srinivasan
Ranch Hand

Joined: Aug 16, 2000
Posts: 99
One more point.
overrinding happens only in inheritance.
But, overloading - within the class the same signature with different flavours.
krishna joginpalli
Greenhorn

Joined: Feb 01, 2001
Posts: 4
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.
krishna joginpalli
Greenhorn

Joined: Feb 01, 2001
Posts: 4
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.
Hungson Le
Ranch Hand

Joined: Jan 25, 2001
Posts: 32
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).]
anurag priya
Ranch Hand

Joined: Dec 19, 2000
Posts: 72
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


Anurag priya
SCJP 2(83%); Brain Bench Certified for: Masters level EJB2.0(97%) - Masters level J2EE (83%) - BEA-WebLogic Application Server8.1 (86% )
Velmurugan Periasamy
Ranch Hand

Joined: Nov 09, 2000
Posts: 95
Overloading - Parametric polymorphism
Overriding - Runtime polymorphism


------------------
Velmurugan Periasamy
Sun Certified Java Programmer
----------------------
Study notes for Sun Java Certification
http://www.geocities.com/velmurugan_p/


------------------------------------------------------------------------------------<BR>Velmurugan Periasamy<BR>Sun Certified Java Programmer for Java 2 Platform<BR>Sun Certified Web Component Developer for J2EE platform<BR>Sybase Certified EAServer Developer (similar to jCert level 3)<BR>------------------------------------------------------------------------------------<BR>Study notes for Sun Java Certification<BR><A HREF="http://www.geocities.com/velmurugan_p/" TARGET=_blank rel="nofollow">http://www.geocities.com/velmurugan_p/</A><BR>------------------------------------------------------------------------------------
Hema Sukumar
Greenhorn

Joined: Dec 22, 2000
Posts: 22
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.
 
jQuery in Action, 2nd edition
 
subject: overriding and polymorphism