Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java overloading and overriding

Tags:

java

oop

Suppose i have two methods in a class say

public void eat(int i,String s) and

public void eat(String s, int i)

then what is it like. Overloading or overriding?

like image 918
Sana Avatar asked Oct 03 '10 06:10

Sana


People also ask

What is difference between function overloading and overriding?

Function overloading is used when we want multiple functions providing a similar implementation. However, function overriding is used when we want to add some additional functionality on top of base class implementation.

Which is better overriding or Overloading?

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.

Why we use Overloading and overriding in Java?

Method overloading is used to increase the readability of the program. Method overriding is used to provide the specific implementation of the method that is already provided by its super class.

What is overriding in Java?

In Java, method overriding occurs when a subclass (child class) has the same method as the parent class. In other words, method overriding occurs when a subclass provides a particular implementation of a method declared by one of its parent classes.


1 Answers

Overloading means two methods or more with the same name but with different parameters just like your example.While Overriding you implement a method from an interface or abstract class so the method in the super class has an implementation and in the subclass has a different one, Still they have the same method name and parameters.

like image 97
Feras Odeh Avatar answered Sep 27 '22 18:09

Feras Odeh