Overloading Methods and Constructors
|Navigate Classes and Objects topic: )|
Method overloading 
In a class, there can be several methods with the same name. However they have to have different signature. The signature of a method is comprised of its name, its parameter types and the order of its parameter. The signature of a method is not comprised of its return type nor its visibility nor its thrown exceptions.
Methods with the same name in a class are called overloaded methods. Overloading methods has no specific benefit but it is useful to mean that several methods do the same things but with different parameters. For example we may have the operation
runAroundThe. We can define two methods with the same name, but different input parameter type:
|Code section 4.22: Method overloading.
The following two method definitions are valid too:
|Code section 4.23: Method overloading with the type order.
Because the type order is different. If both input parameters were type String, that would be a problem since the compiler would not be able to distinguish between the two:
|Code section 4.24: Bad method overloading.
The compiler would give an error for the following method definitions as well:
|Code section 4.25: Another bad method overloading.
Note, the return type is not part of the unique signature. Why not? The reason is that a method can be called without assigning its return value to a variable. This feature came from C and C++. So for the call:
|Code section 4.26: Ambiguous method call.
the compiler would not know which method to call. It is the same with the thrown exceptions.
Constructor overloading 
The constructor can be overloaded. You can define more than one constructor with different parameters. For example:
|Code listing 4.11: Constructors.
In the code listing 4.11, we defined two constructors, one with no input parameter, and one with one input parameter. You may ask which constructor will be called. Its depends how the object is created with the
new keyword. See below:
|Code section 4.27: Constructor calls.
In the code section 4.27, we created two objects from the same class, or we can also say that
obj2 both have the same type. The difference between the two is that in the first one the
memberField field is not initialized, in the second one that is initialized to
"Init Value". A constructor may also be called from another constructor, see below:
|Code listing 4.12: Constructor pooling.
In the code listing 4.12, the constructor with no input parameter calls the other constructor with the default initial value. This gives an option to the user, to create the object with the default value or create the object with a specified value. The first constructor could have been written using the
this keyword as well:
|Code section 4.28: Another constructor pooling.
Such a call reduces the code repitition. However, this call must be the first instruction of a constructor or else a compiler error will occur.
Method overriding 
To easily remember what can be done in method overriding, keep in mind that all you can do on an object of a class outside this class, you can do it also on an object of a subclass, but the behavior can change.
Although a method signature has to be unique inside a class, the same method signature can be defined in different classes. If we define a method that exists in the super class then we override the super class method. It is called method overriding. This is different from method overloading. Method overloading happens with methods with the same name different signature. Method overriding happens with same name, same signature between inherited classes.
The return type can cause the same problem we saw above. When we override a super class method the return type also must be the same. In fact if that is not the same, the compiler will give you an error.
|If, for example, a class declares two public methods with the same name, and a subclass overrides one of them, the subclass still inherits the other method. In this respect, the Java programming language differs from C++.|
Method overriding is related dynamic linking, or runtime binding. In order for the Method Overriding to work, the method call that is going to be called can not be determined at compilation time. It will be decided at runtime, and will be looked up in a table.
|Code section 4.29: Runtime binding.
In the code section 4.29, the expression at line 3 is true if it is executed a morning and false if it is executed an afternoon. Thus, the instance of
obj will be a
MyClass or a
SubOfMyClass depending on the execution time. So it is impossible to determine the method address at compile time. Because the
obj reference can point to object and all its sub object, and that will be known only at runtime, a table needs to be kept with all the possible method addresses to be called.
Also another rule is that when you do an override, the visibility of the new method that overrides the super class method can not be reduced. The visibility can be increased, however. So if the super class method visibility is
public, the override method can not be
An override method must throw the same exceptions as the super class, or their subexceptions.
|A common mistake to think that if we can override methods, we could also override member variables. This is not the case, as it is useless. You can redefine a variable that is private in the super class as such a variable is not visible.|