Overloading
- Method overloading is always enforced at Compile time
- Overloading has nothing to do with Polymorphism but, it being enforced at compile time, makes it quite much similar to what static polymorphism achieves.
Method Overloading Rules
Method overloading comes with some rules, some being mandatory and some being optional.
Two or more methods are said to be Overloaded if they follow the below MANDATORY rules
- They must have the same method name
- They must have different arguments - this can either be
- Different type of arguments
- Different number of arguments
- Combination of both
And if both mandatory rules are applied on the methods, then they may or may not
- Have different return types
- Have different access modifiers
- Throw different types of checked and unchecked exceptions
Method overloading also works with inheritance. If the parent class has a method and the child class also has a method following the two mandatory rules, then these two methods are said to be overloaded as the child class by default inherits all the methods from the parent class (till they are not overridden); the parent class methods are invisible but yes they do exist.
For examples for Overloading, please refer to the below github link
https://github.com/omerhashmininjago/java-programs/tree/master/src/main/java/com/demonstrate/concepts/overloading/example1
Method Overloading with Autoboxing and Widening
There are two types of parameters that a method can take
- Primitive data types
- Wrapper data types - Also referred to Reference types
In case of method overloading, the compiler searches for the method with the same data type(s)
- in case the same primitive data type is not found in any method definition, then it moves to the next wider primitive data type
- if we are using a Wrapper data type and the compiler is not able to find it, then it will start searching a method having corresponding primitive data type - if that is also not found, then it will look for a wider primitive data type (it will not go next wider Wrapper data type)
- If we are using a Wrapper data type and the compiler is not able to find it, then if a method exists with a data type of its parent class, then it will not go for corresponding primitive data type method, rather it will go with the method with the Parent data type
Examples for all above scenarios will be found on the below link on github
https://github.com/omerhashmininjago/java-programs/tree/master/src/main/java/com/demonstrate/concepts/overloading/autoboxing/widening
Overriding
- When we extend a class, by default all the methods which are visible to the child class are automatically/by default visible to child class. Such methods are referred to as Derived methods.
Now if the child class does not want to use any specific behavior of the parent class, it can simply define that method by its own (making sure that the method signature remains intact) and give its own behavior. This is known as overriding. - Method overriding is referred to as dynamic polymorphism, as the JVM decides at runtime what behavior is to be called for - this is a concept known as Realization at runtime.
Method Overriding Rules
Similar to overloading, overriding also comes with some rules.
The overriding method must follow the below MANDATORY rules
- Should have the same method name
- Should have the same argument list
- Should have the same return type - but starting Java 5, the return type can also be a subclass
- Must not have a more restrictive access modifier -> if parent has the return type as protected, then the child cannot have private
- Must not throw a new or broader checked exception
And if the overriding method is following all the above rules
- It can have a less restrictive access modifier
- Can throw a narrower checked exception
For overriding examples, please refer to below github link
https://github.com/omerhashmininjago/java-programs/tree/master/src/main/java/com/demonstrate/concepts/polymorphism/example1
No comments:
Post a Comment