Blog Image

Method overloading and method overriding in java 

Basicaly, methods means it is collection of statement or a group of statement. using methods we can create a big things in small small parts and then work that small small parts. it is used to reduce the complex program in small parts. 


What is the Method Overloading in java?


Method overloading in java means we have one or more methods with the same name and differrent parameters (logic same but differrent parameters) .


Whenever our programming logic is same but parameters are differrent then it is called method overloading in java.


we can also say method overloading as early binding and comilpe time polymorphism. method resolution happens at a compile time means which method call first.


Rules of method overlaoding:


a.method name is same


b.method parameters are differrent


c.return type are same or differrent


d.access specifier are same or differrent.


Use of method overlaoding:


1.Method Readability.


2.Code readability.


Example:


public class Addition{


public static void main(String []args){


Addition.add(10);


Addition.add(12,12);


}


public static void add(int a){


System.out.println(a);


}


public static void add(int a, int b){


System.out.println(a+b);


}


}


in above program method logic is same but parameters are differrent.


What is method overriding in java?


Method overriding in java means redefine the parent class method to child class, but when user is not satisfy with the parent class method implementation then user change the method implementation as per requirement. 


when user needs another class method then user extend that class in that class with the same logic and same implementation then it is called as method overriding.


Rules of method overriding


1.method name is same


2.method signature are same.


3.method access specifier are same or in increasing scope.


4.method return type are same.


also we can say method overriding as a late binding , run time polymorphism in java. Late binding menas which method call decides at run time.


Example:


public class Test{


public String m1(){


System.out.println("Hello Sam");


}


public class Demo extends Test{


public String m1(){


System.out.println("Hello Ram"); //change the logic but not any signature.


}


}


FAQ(Method Overriding and Method Overloading in java)


 


1.Why we can say overloading as static polymorphism?


method resolution happens at a compile time but, The method called is based on the reference type, not the object type.


2.Why do we call overloading is compile time polymorphism?


in method overloading method resolution happens at compile time, not run time.



 3.What is the purpose of overriding?


The purpose of method overriding in Java is to allow a subclass to provide a specific implementation of a method that is already defined in its superclass. 


 4.What is the difference between overloading and overriding?


Overloading


1.Happens within a single class 


2.Methods have the same name but different parameter types or numbers 


3.method resolution happens at compile time.


Overriding


1.Occurs between a parent class and its subclass 


2.Methods have the same name, same parameter types, and same return type in both classes 


3.method resolution happens at run time.


5.How can we stop overriding?


we can stop overriding declared method as private or final.


6.Can we override private method?(why)


No, we cannot override private methods in Java.


because the scope of private methods is limited to the class and we cannot access them outside of the class .

Author Photo

Samadhan Shinde

Batch Number: Java Batch - 5