Topic: 请大大侠详细解释一下override和overload的概念,以及多态的概念

  Print this page

1.请大大侠详细解释一下override和overload的概念,以及多态的概念 Copy to clipboard
Posted by: wmgreat
Posted on: 2003-01-10 09:06

请大大侠详细解释一下override和overload的概念,以及多态的概念

2.Re:请大大侠详细解释一下override和overload的概念,以及多态的概念 [Re: wmgreat] Copy to clipboard
Posted by: rainman
Posted on: 2003-01-10 09:55

现弄个中文翻译:
override : 重载,这个重(chong2)含义是重叠,覆盖(基类方法)
overload: 过载,这个过,难道是过度,过量?本来已经有了一个方法,现在又做一个相同名字,返回类型的方法,但接受的参数不一样。

3.Re:请大大侠详细解释一下override和overload的概念,以及多态的概念 [Re: wmgreat] Copy to clipboard
Posted by: scottding
Posted on: 2003-01-10 10:15

hoho,rainman解释的比较精辟。

4.Re:请大大侠详细解释一下override和overload的概念,以及多态的概念 [Re: rainman] Copy to clipboard
Posted by: mitnickcbc
Posted on: 2003-01-12 21:10

rainman wrote:
现弄个中文翻译:
override : 重载,这个重(chong2)含义是重叠,覆盖(基类方法)
overload: 过载,这个过,难道是过度,过量?本来已经有了一个方法,现在又做一个相同名字,返回类型的方法,但接受的参数不一样。


我看到的翻译大多是:
override 覆盖
overload 重载

5.Re:请大大侠详细解释一下override和overload的概念,以及多态的概念 [Re: wmgreat] Copy to clipboard
Posted by: floater
Posted on: 2003-01-12 22:59

I think it would be better to treat "override" as "overwrite". So
override - overwrite the same signature of the method(method name + method parameters)
overload - overload the same method name, but with different passing parameters.

Also, overridding happens between child classes and parent classes. And overloading can happen in the same class.

6.Re:请大大侠详细解释一下override和overload的概念,以及多态的概念 [Re: wmgreat] Copy to clipboard
Posted by: javachina
Posted on: 2003-01-18 22:57

mitnickcbc quoted translation is more accurate.

Read more here, Methods Overloading and Overriding
Detailed explanations and many examples

http://bobcat.webappcabaret.net/javachina/faq/02.htm#Overload_

7.Re:请大大侠详细解释一下override和overload的概念,以及多态的概念 [Re: wmgreat] Copy to clipboard
Posted by: javait
Posted on: 2003-01-24 00:44

mitnickcbc quoted translation is good!
the key point is the "signature" of that class.


ApprovedApproved

-JavaIT

8.Re:请大大侠详细解释一下override和overload的概念,以及多态的概念 [Re: javait] Copy to clipboard
Posted by: javachina
Posted on: 2003-01-24 10:22

javait wrote:
the key point is the "signature" of that class.

I think you mean the "signature" of that method. Smile

=====================================
Q. What is the basic difference between the concept of method overloading and overriding?
A: They are totally irrelevant except the name similarities in two senses.
sense #1:
English words overriding and overloading both have "over" inside
sense #2:
overloading methods will have the "same name" but different signature
overriding methods will have the "same name" and same signature

Except these above, there is nothing in common between them.

Q. What are the differences between overloading and overriding? Are they both concepts of polymorphism?
A: No, overloading is NOT a concept of polymorphism.
Let me try a short one about overloading. The approach is like you take the test, cross out something obviously not correct, before you decide what is correct.
3 + 5
3.0 + 5.0
The plus (+) operator is overloaded long before even OO concepts came into play in computer sciences. It is in Fortran (the first high level computer language) for sure.

American drives on the right. British drives on the left.
Maha Anna is right on this topic.
I'll be right back.
These are right angle triangles.
You have the right to disagree with me.
He is a right extremist.
Actually, you can even overload the English word right to mean a dog in certain communication circle. Just thinking a scenario, you name your dog as Right, then in your family, when you or your kids talk about Right, the most of the time, it is referring to the dog. And it is a valid overloading; no body will put you in jail or even criticize you for your use of English word right this way...
...
The English word right is overloaded here. All natural languages are overloaded long before computer even invented. We understand the difference by the context. Compiler understands the method overloading by the signature, which serves the same purpose as context, but more reliable, since compiler is not as intelligent as you are. Smile

Polymorphism is a very specific OO concept, which has nothing to do with overloading. You really need to read a good OO book to understand it. It is too large a job for me to write about it here.

There is so much misunderstanding out there on this topic, a lot of them are in published books. Like Marcus said: "Don't believe everything you read."

If you don't believe what you read here by Roseanne, I'm OK.

P.S. The word Polymorphism itself in natural languages can be overloaded too like the Right example in English. You can overload the word polymorphism with whatever meaning you want it to mean. However, I still agree with most computer scientists' narrow and precise definition of polymorphism. If you don't agree with me, no more arguments are needed; we all can rest in peace...
==================================
Written by myself, and you can see the original from

http://bobcat.webappcabaret.net/javachina/faq/02.htm#Overload_

9.Re:请大大侠详细解释一下override和overload的概念,以及多态的概念 [Re: wmgreat] Copy to clipboard
Posted by: Biubiu
Posted on: 2003-01-24 12:57

看看四人帮的设计模式的第一章关于面向对象概念的阐述,保证让你茅塞顿开。Smile

10.Re:请大大侠详细解释一下override和overload的概念,以及多态的概念 [Re: wmgreat] Copy to clipboard
Posted by: jackycct
Posted on: 2003-02-13 14:07

Example of overloading :
i. public void setValue(int pValue)
ii. public void setValue(float pValue)
If you call the method with value 0, it will call the method i.
If you call the method with value 0.0, it will call the method i.

----------------------------------------------------------------------------------------------------------
Example of override :
Class A is the super class of Class B, and both classes have the method setValue(int pValue).
public class A {
public void setValue(int pValue) { ... }; // i.
}
public class B extends A {
public void setValue(int pValue) { ... }; // ii.
}
so if you have an object reference of Class B, method ii will be called.
if the reference is Class A, method i will be called.

11.Re:请大大侠详细解释一下override和overload的概念,以及多态的概念 [Re: wmgreat] Copy to clipboard
Posted by: javachina
Posted on: 2003-02-14 08:48

jackycct said:
if you have an object reference of Class B, method ii will be called.
if the reference is Class A, method i will be called.


What you said is wrong. It is NOT the object reference which decides which method to call. It is the Object itself which decides which method to call. ---- Dynamic Binding

A a = new B();

a is a reference of A, but the real Object is B. Since B is an A. AppleTree is a Tree. B is an A.

On the opposite on fields, It is the object reference which decides which field to bind. ---- Static Binding

Run the following example, you will see the difference.
        // TestBinding.java
class Base {
String s = "string in Base";
void init(){
System.out.println("init() call in Base");

}
}
class SubBase extends Base{
String s = "string in SubBase";
void init(){
System.out.println("init() call in SubBase");

}
}

public class TestBinding {
public static void main(String[] args) {
Base b = new SubBase();

// static binding on instance fields
System.out.println(b.s); //string in Base

// dynamic binding on instance methods
b.init(); //init() call in SubBase
}
}

More explanation and sample code here
http://bobcat.webappcabaret.net/javachina/faq/03.htm#inh_Q109

12.Re:请大大侠详细解释一下override和overload的概念,以及多态的概念 [Re: wmgreat] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-11-30 17:13

挖贴行动中


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923