Overloading VS Overriding

OVERRIDING VS. OVERLOADING IN JAVA

0  0 2016-05-13 08:33:03

Overriding vs. Overloading in Java

1. Definitions
The Overloading occurs when two or more methods in one (single) class have the same method name but different parameters or variables. Overriding means having two methods with the same method name and parameters. One of the methods is in the parent or main class and the other is in the child or sub class. Overriding allows a child or sub class to provide a specific operation or implementation of a method that is already provided its parent or main class.

2. Overriding vs. Overloading
Facts:

1) The real object type in the run-time, not the reference variable's type, determines which overridden method is used at only runtime. In comparison, reference type determines which overloaded method will be used at only compile time.
2) Polymorphism applies only to overriding. Polymorphism not applies to overloading.
3) Overriding is a run-time approach while overloading is a compile-time concept.

3. An Example of Overriding

class Dog{
publicvoid bark(){
System.out.println("woof ");
}
}

class Hound extends Dog{
publicvoid sniff(){
System.out.println("sniff ");
}

publicvoid bark(){
System.out.println("bowl");
}
}

publicclass OverridingTest{
publicstaticvoid main(String[] args){

        Dog dog =new Hound();
        dog.bark();
}
}

Output:
bowl

In theabove example, the dog variable is announced 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 Java Virtual Machine (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

class Dog{
publicvoid bark(){
System.out.println("woof ");
}

//overloading method
publicvoid bark(int num){
        for(int i=0; i<num; i++)
               System.out.println("woof ");
}
}

In this above example of overloading, the two bark method can be appeal to by using different parameters. Compiler knows they are different. Because of they have different method signature, means method name and method parameter list.

 

OVERRIDING HAPPENS AT RUN TIME :-

Important point to remember is that overriding is a run time phenomenon. Not a compile time phenomenon like method overloading.

 

OVERLOADING HAPPENS AT COMPILE TIME :-

Important point to remember is that overloading is a compile time phenomenon. Means that the compiler determines whether a given method is correctly overloaded, and if not a compiler error is returns as shown in the above example.

Overriding happens at runtime and Overloading happens at compile-time. The irrevocable of overloaded method call to its explanation has happens at compile-time. However irrevocable of overridden method call to its explanation happens at runtime.

Static methods can be overloaded. Which mean a class can have two or more than static method of same name. Static methods can’t be override. Even if you define static method in child class it has nothing to do with the same method of parent or main class.

The most basically difference is that overloading is being done. And in the same class while for overriding base or main and child or sub classes are required. Overriding is all about giving a specific implementation to the inherited methods of parent or main class.

Static irrevocable is being used for overloaded methods. And dynamic irrevocable is being used for override or overriding methods.

Comments (0)

Post Your Comments

Headquarter



Hong Kong Office



Find Us On

Ziuby
Ziuby