Method overloading
In a class you can write more than one method with the same name by changing the parameters as follows: --------- i.e.
• Types of parameters
• Order of parameters
- Overloading methods
- Overloading constructors
Explanation:
test () is overloaded four times. the first version takes no parameter the second takes one integer parameter and the third takes two integer parameters and the fourth takes one double parameter
Method invoking depends on parameter.
When you overriding methods, return type is not a matter i.e. you can use any return type which will not be used in method invocation.
Constructor overloading: -----writing multiple constructors in a class by changing the parameter is called constructor overloading.
Return type: ------------no need.
METHOD OVERRIDING
Implementing the super class method in subclass with the same name and with the same signature. This is called as method overriding.
Rules for method overriding: --------
• Subclass parameters must be same as super class parameter.
• Subclass method return type must be same as super class method return type.
• Subclass method access specifier must be same as super class method access specifier or higher than super class method specifier
When super class method is throwing an exception in subclass, then you do one of following: --
• You can use same method level exception in subclass
• Sub class methods can throw subclasses to super class exception.
• Subclass method can’t throw super class to super class exception class.
Assume that super class has a method of follows: ---------
Void m1(int x)throws B
You can override this method with one of the following form: ----
Void m1(int x)[ protected void m1(int a)throws B]
Void m1(int x)[ public void m1(int a)throws B]