• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I kept on mixing up these terms. Can anyone help me understand these better. I know these questions might sound silly but thanks anyway.
1. whats the difference between overloading and overidding
2. difference between polymorphism and inheritance
3. which one does polymorphism do (overloading or overriding)
Pearl
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These questions are not silly and have been asked multiple times. The link to the "search" is just below the two buttons next to the moose.

1. whats the difference between overloading and overidding

Overloading: Same method name, different signature, may be in the same class or a different class.

Overriding: Same method name, same signature, in a different class

2. difference between polymorphism and inheritance

Have you read How my Dog learned Polymorphism?

3. which one does polymorphism do (overloading or overriding)

Overriding
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//some supplement from luco, plse see //** style
//comments, thx a lot.
These questions are not silly and have been asked multiple times. The link to the "search" is just below the two buttons next to the moose.
1. whats the difference between overloading and overidding
Overloading: Same method name, different signature, may be in the same class or a different class.
Overriding: Same method name, same signature, in a different class
//**In a sub-class may be more accurate?
2. difference between polymorphism and inheritance
Have you read How my Dog learned Polymorphism?
3. which one does polymorphism do (overloading or overriding)
Overriding
//**Overloading does part of polymorhism.
Thanks,
luco zhao
 
Pearl Pe
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks alot for the quick response.
I'm currently looking at the link you posted
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two types of polymorphism. One is Static polymorphism and another on eis dynamic polymorphism. Overloading is staic poymorphism and overriding is dynamic polymorphism.
Thank you,
 
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marilyn deQueiroz:

3. which one does polymorphism do (overloading or overriding)

Overriding


Not sure I completely agree with this, or at least only if qualified "in the specific case of Java". Doesn't really capture signature-based method polymorphism, which is definitely an overloading issue. If memory serves, the difference is more apparent in C++ than in Java; in the former I think signature-based polymorphism is a runtime issue, in Java it is completely a compile-time issue due to the semantics of method-call conversion of references. You still have signature-based polymorphism; it is just a dangerous thing to depend upon in Java.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the purposes of the exam, overriding is the only form of polymorphism.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As JLS states, an instance method can be overridden (not hidden) by an instance method only. And a static method can be hidden (not overridden) by a static method only.
Would someone please tell me what the diff. between "overridden" and "hidden" is? What's the diff. when they are implemented actually in compile-time and runtime??? Thanks a lot.
[ May 03, 2002: Message edited by: Leif Huang ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Leif Huang:
Would someone please tell me what the diff. between "overriden" and "hidden" is?


Overriding a method deals with polymorphism, hiding does not.
One other difference is that, when you override a method, you can still invoke the overridden method by using super.methodName(). However, when you hide a method, this is not possible.
Let's take a look at an example:

If you look at the first two sets of outputs, you can see a key difference. When invoking an overridden method (doIt), dynamic-binding is used to determine which method to call. In other words, the run-time type of the variable is used. That's why, even though p2 is of type Parent, Child.doIt() is invoked. At run-time, p2 references a Child object.
However, if you look at the second set of output, you can see that, when using a hidden method (staticDoIt), dynamic-binding is not used. Rather, the compile-time type is used. That's why Parent.staticDoIt() is invoked.
I suppose this doesn't really explain the difference between overriding and hiding, but it does describe some of the implications of one or the other. If you're still confused, let me know.
Corey
 
Leif Huang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Corey, Thanks.
I still have some questions:
(1) A static method can not be overridden. is the reason because it is implicity "final" as stated in JLS? Why is it implicity "final"? As I known, "final" is just an ADD_ON tag for static methods, I mean they can be either final or not.
(2) when a static method is hidden, super.staticMethod() does not work. Is the reason because it is a static method? or Java's rule? other reason?
(3) Can I "hiding" an instance method so that late-binding is not applied? It can be resolved in compile-time.
Regards
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

(1) A static method can not be overridden. is the reason because it is implicity "final" as stated in JLS? Why is it implicity "final"? As I known, "final" is just an ADD_ON tag for static methods, I mean they can be either final or not.


Static methods are "class methods." This is something that can be done by that class, not by objects of that class. In that respect, overriding doesn't make much sense. When you override a method, you're saying that any instances of a subclass can provide their own implementation of this method, but a static method isn't an instance method - it's a class method. Where did you read that static methods are implicitly final? That's not true! In fact, there is a distinct difference between final static methods and non-final static methods; final static methods can't be hidden.
From the JLS, §8.4.3.3 final Methods:


A method can be declared final to prevent subclasses from overriding or hiding it. It is a compile-time error to attempt to override or hide a final method.


Next question...

(2) when a static method is hidden, super.staticMethod() does not work. Is the reason because it is a static method? or Java's rule? other reason?


From the JLS, §8.4.3.2 static Methods:


An attempt to reference the current object using the keyword this or the keyword super in the body of a class method results in a compile-time error.


A static method can be called without creating an instance of the class (as well as an instance of the superclass) so the keywords this and super are meaningless in those contexts as they specifically refer to the current instance and the parent class of the current instance.

(3) Can I "hiding" an instance method so that late-binding is not applied? It can be resolved in compile-time.


If you supply an instance method in a subclass with an identical signature as an instance method in a superclass, the method is said to be overridden - hiding does not apply to instance methods.
I hope that helps,
Corey
 
Leif Huang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Corey,
(1)You are right. it is not true that "a static method is implicitly final". I read the wrong statement from HERE. I did not double check it, Sorry about that?
(2)From the JLS $8.4.3.2

An attempt to reference the current object using the keyword this or the keyword super in the body of a class method results in a compile-time error.


I think the JLS says you can not use keyword this or super in the BODY of a class (static) method since you can not use any instance context in a static context. But if you use them in an instance method, super.doIt() or super.staticDoIt() both are OK since a static method could be called either by an instance var or by the class name as long as it is not in a static context.
(3) I am still confused about the overridden and hidden. I can answer correctly from the output of your sample program. I understand why the overriding methods are computed at runtime by the object type because they need to achieve dynamic polymorphism. But I still do not understand why the static methods are hidden, are not overridden and computed at compile-time (hiding of fields too) by the reference type. Do you know the design concept behind?
Thanks and Regards
Leif
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Leif Huang:
But if you use them in an instance method, super.doIt() or super.staticDoIt() both are OK since a static method could be called either by an instance var or by the class name as long as it is not in a static context.


Correct - that's why p1.staticDoIt() works in the code sample above. From the JLS, §15.12.4.4 Locate Method to Invoke:


If the invocation mode is static, no target reference is needed and overriding is not allowed. Method m of class T is the one to be invoked.



I am still confused about the overridden and hidden. I can answer correctly from the output of your sample program. I understand why the overriding methods are computed at runtime by the object type because they need to achieve dynamic polymorphism. But I still do not understand why the static methods are hidden, are not overridden and computed at compile-time (hiding of fields too) by the reference type. Do you know the design concept behind?


I think you're thinking of instance methods and class methods as one in the same thing. Rather, they are very different. Static methods are behaviors that can be perfored without an instance of an object. Look at the Math class, as an example. This class has all static methods. That means that they can be performed without creating an instance of the Math class.
Polymorphism, inherently, has to do with instances of classes. The correct method to be invoked depends upon the object, not the class. In order to have that work, you must have an object to begin with - hence, this is only applicable to instance methods. Static methods can be invoked without a instance at all.
Basically, what you're asking about is "Why can't we use polymorphism with static methods?" The reason is that it makes no sense? If you can invoke a static method without creating an instance, how would you ever determine which method is the correct method to invoke?
I hope that helps,
Corey
 
Leif Huang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I say "It is no problem to implement polymorphism with static methods if the language designer like to do so, but it doesn't make sense and is not necessary." Correct?
Thanks a lot
Leif
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key is that it just doesn't make logical sense. I don't know how you'd implement it even if you tried.
Corey
 
reply
    Bookmark Topic Watch Topic
  • New Topic