top of page
Search

Java - Function Overloading Vs Function Overriding

What is signature of a function?

Signature of a function identifies every function uniquely. It includes name of the function (method), list of parameters and their types.

What is polymorphism?

When the objects of different types has common behavior (method) but implementation of this behavior differs , it is called polymorphism. In other words, objects of different types may have same method but its implementation differs.

For example, if we consider three classes Book, Periodicals and NewsPaper, all of these three classes may have a method implemented getEdition() which returns an edition, means same behavior (purpose).However, implementation of this method differs. New edition of book usually comes after a gap of year in general, but the edition of periodicals is published weekly, monthly basis. The newspaper edition changes on daily basis.

This is the concept of polymorphism.

Is the function overloading a static polymorphism?

Yes. Function overloading is a static polymorphism as the call is resolved at compile tile. Compiler binds a call with a appropriate function (method).

Is the function overriding a dynamic polymorphism?

Yes. Function call can be resolved only during execution of program on the basis of type of object on which function/method is called. Due to inheritance, it is possible that the base class reference may hold derived class object or base class object depending on run-time situation. Hence, calls to methods which are common to base and derived class cannot be resolved at compile time.

Example: If base class is Employee and derived class is Manager, then following conditional execution happens during execution because of which call to function cannot be resolved.

if ( choice == 1)

Employee e = new Manager();

else

Employee e = new Employee();

e.calculateSalary() // this method is overriden

Differential between Function Overloading and Function Overriding.

Function Overloading

Function Overriding

Signature of a function differ by parameter list and type. Name of the function is same.

Signature of function is same in base class and derived class.

Its a static / compile time polymorphism, as the compiler can associate function call with appropriate function by comparing its signature.

Its a dynamic/compile time polymorphism, as the signature of function is same, selection of appropriate function depends on type of object.

Function overloading is possible in the same class or any class.

Function overloading is possible only in derived class. It is applicable only in case of inheritance.

Function overloading is associated with class as well as object. Calls can be resolved with respect to classes or objects.

Function overriding is associated with object, which means calls are resolved by checking type of object.

Static methods/functions can be overloaded.

Static methods/functions cannot be overridden as they are not associated with object but with class.

Constructors can be overloaded.

Constructors cannot be overridden.

Function overloading is more faster than function overriding due to compile time binding.

Function overriding is slow , as calls are resoled during execution.

To request for session on this topic , WhatsApp on 9422317065.

Want to read more?

Subscribe to questionbankos.com to keep reading this exclusive post.

 
 
 

Recent Posts

See All
Programming Using C++

Course Fee - 1200/- (Rupees Twelve Hundred Only) OR pay Rs. 10/- (Rupees Ten Only) as a session fee to join any session of your choice. Sessions are only for Members of this educational web site. Yo

 
 
 
Sorting Technique - Counting Sort

Q.1) How Counting Sort works? Counting sort maintains a separate array to count occurrences of each element and stores them in another array as per their sorted position which is calculates on the bas

 
 
 
bottom of page