Java OOPS – Part 4 – Polymorphism – method overriding & overloading

Polymorphism –

Polymorphism means – one in many forms

Creating multiple methods / functions with the same name but performing different task, same way creating single object or reference variable referring to multiple classes.

Polymorphism can be achieved in 2 ways

  •   Method Overloading (Compile time polymorphism or static binding)
  •   Method Overriding (Run time polymorphism or dynamic binding)

For details, watch here

Method Overloading

To overload methods –

  • Method name should be same in the same class.
  • Method signature should be unique for each method with same name, signature means
    • No. of parameters
    • Parameter data type
    • Return type

Call to method resolved during compile time by compiler, that is why called as static or early binding or compile time polymorphism

Static binding as actual object of the class is not used to bind. [happens with in same class]

Private, static and final methods can be over loaded.

 

Method Overriding

To override methods –

  • Method name should be same in the parent and all of it child classes
  • Method signature should be same for each method with same name, signature means
    • No. of parameters
    • Parameter data type
    • Return type

Call to method resolved during run time, that is why called as dynamic or late binding or run time polymorphism

Dynamic binding as binding happens by using actual object [call to method happens by using object]

Private, static and final methods can not be overriden.

What is binding –

Link between method call to method definition is called as binding.

Content posted is based on learning or working experience,
Share the post with others if find useful,
Any queries?, leave comments or discuss on our facebook:qavalidation.com, Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *