Reputation: 123
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?
Upvotes: 10
Views: 8451
Reputation: 49
Overloading : In Java Overloading is a Process where a class Contains multiple implementation of methods or constructor by differentiating there no of arguments or types of arguments.
public class TestDemo
{
public void disp(String c)
{
System.out.println(c);
}
public void disp(String c, int n)
{
System.out.println(c+" "+n);
} }
class Sample
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
TestDemo obj=new TestDemo();
obj.disp("a");
obj.disp("a",2);
}}
Overriding : Here Methods can be over riding. It must be happen within two class. One is parent & another is child. In this parent class’s method with same name and signature.
public class TestDemo
{
public void eat()
{
System.out.println("Human is Eating");
}
}
class Overriding extends TestDemo
{
public void eat()
{
System.out.println("Human is Eating");
}
public static void main(String[] args)
{
Overriding obj=new Overriding();
obj.eat();
}
}
for more knowledge click on this link : https://arzatechs.com/overloading-and-overriding-in-java/
Upvotes: 1
Reputation: 15413
In this specific case, it's overloading.
Let's differentiate both terms a bit dipper:
Overloading (same function name but different signature):
Overriding (same function with the same signature):
Visit to learn more about overloading and overriding.
Upvotes: 0
Reputation: 458
OVERLOADING:
Two or more methods with same name but different parameters in same class. The same as your question situation.
public void eat(int i,String s)
public void eat(String s, int i)
So, in your case it is method Overloading.
OVERRIDING:
ex.
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
}
Upvotes: 0
Reputation: 115
Kind of rules about overloading and overriding:
*Taken from Sun Certified Programmer for Java 6 Study Guide by Kathy Seirra, Bert Bates.
Upvotes: 2
Reputation: 4537
Method Overloading simply means providing two separate methods in a class with the same name but different arguments while method return type may or may not be different which allows us to reuse the same method name.
Method Overriding means defining a method in the child class which is already defined in the parent class with same method signature i.e same name, arguments and return type.
Difference Between Method Overloading and Method Overriding
For more details, you can read Everything About Method Overloading Vs Method Overriding.
Upvotes: 2
Reputation: 832
Property ------------------- OverLoading -------------------- Overriding
Method Names -------------->must be Same--------------------> must be same
Arg Types------------------>must be Different(atleast arg)---->must be same(Including Order)
Method Signature----------->must be Different(atleast arg)---->must be same(Including Order)
Return Type---------------->No restriction------------------->No restriction
Private,Static,Final------->No Restriction-------------------->Must be Same
Access Modifyer------------>No Restriction--------------------Same
try/Catch----------------->No Restriction--------------------Exception Is thrown
Method Resolution-------->Compiler time (Reference)--------(JVM) Run Time Poymorphism
Upvotes: -1
Reputation: 1492
This is overloading. Overriding is used in inheritance when you give different implementation to the same method signature.
Upvotes: 6
Reputation: 2205
That's overloading. In brief:
Overriding = replacing
Overloading = give more options
Upvotes: 3
Reputation: 454922
That would be method overloading, as it meets the conditions for method overloading:
Also overriding can happen only when inheritance is involved. Since both of your methods are in the same class it cannot be overriding.
Upvotes: 7
Reputation: 9296
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.
Upvotes: 13