method overloading vs method overriding

method overloading vs method overriding

Method overloading and method overriding are important topics in Java, Difference between method overloading and method overriding is given below.

PropertyMethod OverloadingMethod Overriding
Method nameMust be sameMust be same
Argument typeMust be different(At least in order)Must be same(Including Order)
Method SignatureMust be differentMust be same
Return TypeNo restrictions Must be the same until Java 1.4v but from Java 1.5v onwards co-variant return type allowed
private,static, final methodCan be overloadedcan be overrided
Access ModifierNo restrictionsThe scope of access modifier cannot be reduced but we can increase
throw clauseNo restrictions If the child class method throws any exceptions then the parent class method must throw the same checked exception as is parent but there are no restrictions for unchecked exception
Method resolutionAlways take care of the compiler based on the reference type Always take care by compiler based on reference type
known asCompile Time Polymorphism/ early binding/ static polymorphismRuntime Polymorphism/Dynamic Polymorphism/Late binding

Note-

In overloading, we have to check only the method name(must be the same) and argument type (must be different). We don’t need to check any other thing like return type, access modifier.

But in overriding we have to check everything like method name, argument type, access modifier, return type, throws class, etc.

Consider the following method in parent class

    public void method(int x)throws IOException 

Now in the child class which of the following method we can take

public  void method(int i)  //Valid
public static void method(long i)  //Valid
  public static void method(int i)  //Invalid
 public  void method(int i)throws Exception  //Invalid
    public static abstract void method(double i)  //Invalid

Similar Java Tutorials

Leave a Comment