Method overloading is the way of implementing static/compile time polymorphism in java. Method overloading means more than one methods in a class with same name but different parameters. Parameters can be differing in types, numbers or order. Compiler resolve method call by matching method signature at compile time, that’s why it is known as static or compile time polymorphism. It is also known as static binding.
Ways to implement method overloading in java:
- Parameters differ in types.
- Parameters differ in number.
- Parameters differ in order.
If a subclass provides a method with the same signature (name and parameter) as in its super class, then subclass overrides the method of its super class. This process of overriding a super class method by subclass is known as method overriding.
Conditions for method overriding:
- Method in subclass must have same signature as in its super class.
- Two classes must follow IS-A relationship.
|Method Overloading||Method Overriding|
|Method overloading increases the readability of the program.||Method overriding provides the specific implementation of the method that is already provided by its super class.|
|Method overloading is occurs within the class.||Method overriding occurs in two classes that have IS-A relationship.|
|Parameter must be different.||Parameter must be same.|
Java interview questions on method overloading and overriding
- What is method overloading in java?
- Can we declare an overloaded method as static and another one as non-static?
- Can overloaded methods be synchronized?
- Can we declare overloaded methods as final?
- Can overloaded method be overridden?
- What is method overriding in java?
- Can static method be overridden?
- Difference between method overloading and overriding in java?
- Can we override private methods in java?
- Is it possible to override non static method as static method?