aks
aks

Reputation: 1859

polymorphism in java

Is method overloading is an example of runtime polymorphism or compile time polymorphism?

Upvotes: 2

Views: 2286

Answers (4)

user1746619
user1746619

Reputation: 83

Method overloading is example of complie time binding. Compiler resolves which method needs to be called at compile time only, So it is compile time polymorphism or you can say static binding, But Method overriding is Runtime polymorphism, bcz at run time , depending on the object reference , method call is resolved.

Upvotes: 0

ApproachingDarknessFish
ApproachingDarknessFish

Reputation: 14313

Method overloading and polymorphism are two completely different concepts. Polymorphism relates to inheritance and determines from which level of the class hierarchy the method is called. Method overloading is creating two methods in the same location with the same name that take different arguments. For instance:

class A{
    public void doSomething(){
         System.out.println("A method");
    }

    //method overloaded with different arguments-- no polymorphism 
    //or inheritance involved!

    public void doSomething(int i){
         System.out.println("A method with argument");
    }
}

class B extends A{

    //method polymorphically overridden by subclass. 

    public void doSomething(){
         System.out.println("B method");
    }
}

//static type is A, dynamic type is B.
A a = new B();
a.doSomething(); //prints "B method" because polymorphism looks up dynamic type 
                 //for method behavior

Upvotes: 2

G.E.B
G.E.B

Reputation: 76

Method overloading is example for compile time binding or static binding method overloading results in polymorphic methods, a method can be defined more than one time having the same name and varying number either method parameters or parameter types (java allows to do this) , when this happens the linkage between method call and actual definition is resolved at compile time thus overloading is called compile time binding or static binding. OO Program language features call it has compile time Polymorphism.

Upvotes: 0

polygenelubricants
polygenelubricants

Reputation: 383686

There is no operator overloading in Java.

Method overriding enables run-time polymorphism; method overloading enables compile-time polymorphism.

You can read more about this in your textbook.

See also

Upvotes: 4

Related Questions