Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding and polymorphism

 
Chun Wang
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Kevin Hou
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think overloading and overriding are both polymorphism
see: http://www.ergoit.co.uk/ObjPoly/WhatWhy.html
 
Kevin Hou
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
another good website for POLYMORPHISM
http://www.fsu.umd.edu/dept/cosc/htracy/cosc390/LM7Polymorphism/PolymJava.html
 
Hungson Le
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more point.
overrinding happens only in inheritance.
But, overloading - within the class the same signature with different flavours.
 
krishna joginpalli
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Velmurugan Periasamy
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overloading - Parametric polymorphism
Overriding - Runtime polymorphism


------------------
Velmurugan Periasamy
Sun Certified Java Programmer
----------------------
Study notes for Sun Java Certification
http://www.geocities.com/velmurugan_p/
 
Hema Sukumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic