Click to See Complete Forum and Search --> : what exactly overridden method means ?[java]


blue_gene
03-25-2004, 03:03 PM
hi, many a times i confuse overriding of methods. can any body explain it clearly.

suppose i have a class call it class A. and i have a child class call it B.

so

class A
{

modifier return_rype somemethod(par1,par2,...) { //code }

}

class B extends A
{

modifier return_rype somemethod(par1,par2,...) { // may be different body }


}


now my question is : by watching what i should call the method has been overridden ?

is it the return type , no of parameters and the name of the method ? if i see these are same between two class then should i term it as overridden method ? does modifier can play role ?

i am not sure about it. can anybody explain when a method will be called overridden. i.e
by observing what i can call a method has been overridden ?

thanks

bwkaz
03-25-2004, 08:30 PM
Any method that's declared with the same signature (signature is method name, number of arguments, and type of each argument -- NOT the return type or the modifier) in a derived class has been overridden in that derived class.

If the number of parameters or the type of any of the parameters is different, then the method has been overloaded, not overridden.

At least, this is the case in both C++ and the .Net framework (yeah, I use it at work, so sue me :p) -- with two exceptions: In .Net, a method that is not declared either "virtual" or "overridable" (which modifier you use depends on the language you use) cannot be overridden. It can still be overloaded, though. And in C++, if you don't declare the method "virtual", then only pointers to (or references to, or instances of) the derived class can call the overridden method. A pointer to the base class type will call the base class version instead.

I am not positive whether the same holds true in Java either, but I think it does.

Choozo
03-26-2004, 09:13 AM
Sun Java Tutorials (http://java.sun.com/docs/books/tutorial/java/javaOO/override.html) has some fairly good examples/explanations on this subject.

blue_gene
03-26-2004, 03:43 PM
hmm...in fact i got a summary The return type, method name, and number and type of the parameters for the overriding method must match those in the overridden method

but what about ordering of parameters? are they need to be in same order ?

for example

void method ( int a, float b ) // in Super class

void method(float a, int b) // in Child class


what do you call it ? is it overridden ?

look, everything is same except the ordering of parameters. so can i call it overridden ?

bwkaz
03-26-2004, 08:04 PM
Yes, they need to be in the same order (otherwise it's an overload).