Overloading methods
1.They appear in the same class or a subclass
2.They have the same name but, have different parameter lists, and can have different return types.
An example of an overloaded method is print() in the java.io.PrintStream class
Overriding methods
It allows a subclass to re-define a method it inherits from it's superclass
overriding methods:
1. It appears in subclasses
2. They have the same name as a superclass method
3. They have the same parameter list as a superclass method
4. They have the same return type as as a superclass method
5. They have the access modifier for the overriding method may not be more restrictive than the access modifier of the superclass method
If the superclass method is public, the overriding method must be public
If the superclass method is protected, the overriding method may be protected or public
If the superclass method is package, the overriding method may be packagage, protected, or public
If the superclass methods is private, it is not inherited and overriding is not an issue
When overriding, you change the method behavior for a derived class.
e.g Clas A
{
Virtual void hi(int a)
{
}
}
Class B:A
{
public overrid void hi(int a)
{
}
}
Overloading simply involves having a method with the same name within the class.
Example for Over loading
Class A
{
class a()
{
}
class a(int a)
{
}
}