Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question" Watch "question" New topic
Author

question

Pearl Pe
Greenhorn

Joined: Apr 25, 2002
Posts: 2
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
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
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


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
luco zhao
Ranch Hand

Joined: Apr 23, 2002
Posts: 50
//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


Great thanks,<br />Luco Zhao
Pearl Pe
Greenhorn

Joined: Apr 25, 2002
Posts: 2
Hey thanks alot for the quick response.
I'm currently looking at the link you posted
Ramu Kalvakuntla
Greenhorn

Joined: Feb 17, 2002
Posts: 5
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,


Ramu Kalvakuntla<br />SCJP,SCWCD,IBM Certified Specialist - IBM VisualAge for Java
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
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.


Reid - SCJP2 (April 2002)
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
For the purposes of the exam, overriding is the only form of polymorphism.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Leif Huang
Greenhorn

Joined: Apr 29, 2002
Posts: 9
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 ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Leif Huang
Greenhorn

Joined: Apr 29, 2002
Posts: 9
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

Joined: Dec 20, 2001
Posts: 3271
(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

Joined: Apr 29, 2002
Posts: 9
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

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Apr 29, 2002
Posts: 9
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

Joined: Dec 20, 2001
Posts: 3271
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question