Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java overloading and overriding

We always say that method overloading is static polymorphism and overriding is runtime polymorphism. What exactly do we mean by static here? Is the call to a method resolved on compiling the code? So whats the difference between normal method call and calling a final method? Which one is linked at compile time?

like image 507
Padmanabh Avatar asked Mar 18 '10 12:03

Padmanabh


People also ask

What is the difference between method overriding and Overloading?

Method overloading is a type of static polymorphism. In Method overloading, we can define multiple methods with the same name but with different parameters. Method Overriding is a mechanism to achieve polymorphism where the super class and the sub-class have same methods, including the parameters and signature.

What is Java Overloading?

Overloading in Java is the ability to define more than one method with the same name in a class. The compiler is able to distinguish between the methods because of their method signatures.

Which is better Overloading or overriding?

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.


2 Answers

Method overloading means making multiple versions of a function based on the inputs. For example:

public Double doSomething(Double x) { ... }
public Object doSomething(Object y) { ... }

The choice of which method to call is made at compile time. For example:

Double obj1 = new Double();
doSomething(obj1); // calls the Double version

Object obj2 = new Object();
doSomething(obj2); // calls the Object version

Object obj3 = new Double();
doSomething(obj3); // calls the Object version because the compilers see the 
                   // type as Object
                   // This makes more sense when you consider something like

public void myMethod(Object o) {
  doSomething(o);
}
myMethod(new Double(5));
// inside the call to myMethod, it sees only that it has an Object
// it can't tell that it's a Double at compile time

Method Overriding means defining a new version of the method by a subclass of the original

class Parent {
  public void myMethod() { ... }
}
class Child extends Parent {
  @Override
  public void myMethod() { ... }
}

Parent p = new Parent();
p.myMethod(); // calls Parent's myMethod

Child c = new Child();
c.myMethod(); // calls Child's myMethod

Parent pc = new Child();
pc.myMethod(); // call's Child's myMethod because the type is checked at runtime
               // rather than compile time

I hope that helps

like image 68
RHSeeger Avatar answered Oct 27 '22 09:10

RHSeeger


Your are right - calls to overloaded methods are realized at compile time. That's why it is static.

Calls to overridden methods are realized at run-time, based on the type on which the method is invoked.

On virtual methods wikipedia says:

In Java, all non-static methods are by default "virtual functions." Only methods marked with the keyword final are non-virtual.

final methods cannot be overridden, so they are realized statically.

Imagine the method:

public String analyze(Interface i) {
     i.analyze();
     return i.getAnalysisDetails();
}

The compiler can't overload this method for all implementations of Interface that can possibly be passed to it.

like image 30
Bozho Avatar answered Oct 27 '22 09:10

Bozho