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?
重载意味着两个或更多具有相同名称但具有不同参数的方法,就像您的示例一样。虽然覆盖您从接口或抽象类实现方法,因此超类中的方法具有实现,而子类中的方法具有不同的实现,它们仍然具有相同的方法名称和参数。
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.
This is overloading. Overriding is used in inheritance when you give different implementation to the same method signature.
Kind of rules about overloading and overriding:
*Taken from Sun Certified Programmer for Java 6 Study Guide by Kathy Seirra, Bert Bates.
That's overloading. In brief:
Overriding = replacing
Overloading = give more options
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 ie 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 .
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/
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...");}
}
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 .
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.