Overloading and Overriding in Java


Most of the interview room question is most asked question every time in interview room question.

We all knows overloading & overriding as polymorphism. and we know all about the little bit about polymorphism. 



1 ) Overloading.


Here, for example, I take one class Test were two methods as the same name. 

class Test{
              public void numb(int i){
              }                                                             //***Overloading.
              public void numb(float i){
              }
}


here, take a class test and contain two methods which are the same name but, are parameters different. means is method same but parameter different,different arguments also may take the multipal argument. different work means you load different data into same method name means that overloading. above class numb() method overload by two different parameters. 


so, Two methods are said to be overloaded if end only if both methods having the same name but different arguments type.


2 ) Overriding.


Example for we takes one class car. and were many methods is there in the class. 

class car{
             public void wheeler(){
                 System.out.println("four wheeler."); 
            }
             public void color(){
                 System.out.println("Black"); // Parent method overridden chiled method
            }
}
class BMW extends car{
           public void color(){
                  System.out.println("Blue");   //parent method overriding by child. Not sufficient by                                                                          parent method. 
           }
}


here , tack two class, car and BMW car.BMW class extends parent class car through inheritance. BMW car not sufficient by parent class car method color. BMW it's own color Blue. so child class method override parent method. so that is known as overriding.


Comments