简体   繁体   中英

Java overloading and overriding

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:

  • Must have different argument lists
  • May have different return types, if argument lists are also different
  • May have different access modifiers
  • May throw different exceptions

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:

  1. Constructors can be overloaded but not overridden.
  2. Abstract methods must be overridden by the first concrete sub-class.
  3. The overriding method:
    • must have same argument list,
    • must have same return type (it can also be a subclass of parent's class' return type,
    • must not have a more restrictive access modifier,
    • may have a less restrictive access modifier,
    • must not throw new or broader checked exceptions,
    • may throw fewer or narrower checked exceptions, or any unchecked exception.
  4. final methods cannot be overridden.
  5. Only inherited methods may be overridden, and remember that private methods are not inherited.
  6. In a subclass use: super.overriddenMethodName() to call superclass' overridden method.
  7. Overloaded methods:
    • must have different argument lists,
    • may have different return types, if argument lists are also different,
    • may have different access modifiers,
    • may throw different exceptions.
  8. Methods from a superclass can be overloaded in a subclass.
  9. Polymorphism applies to overriding but not to overloading.
  10. Object type (not the reference variable's type) determines which overridden method is used at runtime.
  11. Reference type determines which overloaded method will be used at compile time.

*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:

  • Overriding means having two methods with the same method name and parameters (ie, method signature). One of the methods is in the parent class and the other is in the child class.

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):

  • Two or more methods having the same name with different arguments in same class.
  • Overloading is used when you want to extend class functionality.
  • Overloading is is a compile time polymorphism.

Overriding (same function with the same signature):

  • Two or more methods having the same method name and same arguments in parent class and child class.
  • Overriding is used when you want to reuse the existing functionality.
  • Overriding is a run time polymorphism.

在此处输入图片说明

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM