by Dinesh Thakur Category: Introduction to Java

Polymorphism gives us the ultimate flexibility in extensibility. The abiltiy to define more than one function with the same name is called Polymorphism. In java,c++ there are two type of polymorphism:

compile time polymorphism (overloading)

runtime polymorphism (overriding).

When you override methods, JVM determines the proper methods to call at the program’s run time, not at the compile time. Overriding occurs when a class method has the same name and signature as a method in parent class.

 

Overloading occurs when several methods have same names with

 

  • Overloading is determined at the compile time.
  • Different method signature and different number or type of parameters.
  • Same method signature but different number of parameters.
  • Same method signature and same number of parameters but of different type
  •  

Example of Overloading
     int add(int a,int b)
     float add(float a,int b)
     float add(int a ,float b)
     void add(float a)
     int add(int a)
     void add(int a)                 //error conflict with the method int add(int a)

 

Example: Overloading

 

Class BookDetails{
            String title;
            String publisher;
            float price;

setBook(String title){
}
setBook(String title, String publisher){
}
setBook(String title, String publisher,float price){
}


}

 

Example: Overriding

 

class BookDetails{
            String title;

setBook(String title){ }

}

class ScienceBook extends BookDetails{

            setBook(String title){}                                             //overriding

setBook(String title, String publisher,float price){ }  //overloading

}

 





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.