In method overloading method name is same and the parameters declaration are different the methods are said to be overloaded and the process of overloading is known as method overloading.
//method overloading in java
void Test(int a)
System.out.println(“a ” +a);
void Test(int a,int b)
System.out.println(“a and b “+a +” ” +b);
double Test(double a)
System.out.println(“double a is “+ a);
public static void main(String args)
Method_overload object = new Method_overload();
a and b 10 20
double a is 15.12345
Overriding and Overloading are two very important concepts in Java. They are confusing for Java novice programmers. This post illustrates their differences by using two simple examples.
Overloading occurs when two or more methods in one class have the same method name but different parameters.
Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class.
2. Overriding vs. Overloading
Here are some important facts about Overriding and Overloading:
1). The real object type in the run-time, not the reference variable’s type, determines which overridden method is used at runtime. In contrast, reference type determines which overloaded method will be used at compile time.
2). Polymorphism applies to overriding, not to overloading.
3). Overriding is a run-time concept while overloading is a compile-time concept.
3. An Example of Overriding
Here is an example of overriding. After reading the code, guess the output.
In the example above, the
dog variable is declared to be a
Dog. During compile time, the compiler checks if the
Dog class has the
bark() method. As long as the
Dog class has the
bark() method, the code compilers. At run-time, a
Hound is created and assigned to
dog. The JVM knows that
dog is referring to the object of
Hound, so it calls the
bark() method of
Hound. This is called Dynamic Polymorphism.
4. An Example of Overloading
In this overloading example, the two
bark method can be invoked by using different parameters. Compiler know they are different because they have different method signature (method name and method parameter list).
Overloading vs Overriding in Java
- Overloading happens at compile-time while Overriding happens at runtime: The binding of overloaded method call to its definition has happens at compile-time however binding of overridden method call to its definition happens at runtime.
- Static methods can be overloaded which means a class can have more than one static method of same name. Static methods cannot be overridden, even if you declare a same static method in child class it has nothing to do with the same method of parent class.
- The most basic difference is that overloading is being done in the same class while for overriding base and child classes are required. Overriding is all about giving a specific implementation to the inherited method of parent class.
- Static binding is being used for overloaded methods and dynamic binding is being used for overridden/overriding methods.
- Performance: Overloading gives better performance compared to overriding. The reason is that the binding of overridden methods is being done at runtime.
- private and final methods can be overloaded but they cannot be overridden. It means a class can have more than one private/final methods of same name but a child class cannot override the private/final methods of their base class.
- Return type of method does not matter in case of method overloading, it can be same or different. However in case of method overriding the overriding method can have more specific return type (refer this).
- Argument list should be different while doing method overloading. Argument list should be same in method Overriding.