classCalculate{void sum (int a,int b){System.out.println("sum is"+(a+b));}void sum (float a,float b){System.out.println("sum is"+(a+b));}Publicstaticvoid main (String[] args){Calculate cal =newCalculate();
cal.sum (8,5);//sum(int a, int b) is method is called.
cal.sum (4.6f,3.8f);//sum(float a, float b) is called.}}
classAnimal{publicvoiddisplayInfo(){System.out.println("I am an animal.");}}classDogextendsAnimal{@OverridepublicvoiddisplayInfo(){System.out.println("I am a dog.");}}classMain{publicstaticvoidmain(String[] args){Dog d1 =newDog();
d1.displayInfo();}}
Method overloading is providing
two separate methods in a classwiththe same name but different arguments,while the method return type
may or may not be different, which
allows us toreuse the same method name.
In my framework==I use implicit wait in Selenium. Implicit wait
is an example of overloading. InImplicit wait
we use different time stamps such as SECONDS,MINUTES,HOURS etc.,Aclass having multiple methods withsame name but different parameters
is called MethodOverloading
publicclassMulti{//Super classpublicvoidmulti(){
………………
}}PublicclassMultiplicationextendsMulti(){Publicvoidmulti(){
………..}Publicstaticvoidmain(String args[]){Multi multiplication =newMultiplication();//Polimorphism is applied
multiplication.multi();// It calls the Sub class add() method}}
MethodOverloading:Access modifier can be same or different,Return-Type can be same or different,ParametersMUST be different,Method name MUST be same,
any method can be overloaded
MethodOverriding:After a method is inherited it is possible tochange
the implantation of the method in the child class.
This concept is called overriding.
Method name,Parameter, and Return-TypeMUST be same
access modifier MUST be same or more visible,MUST happen in the sub class,ONLY the instance methods can be overridden
@Override annotation MUST be applicable.
Static and Constructor cannot be override.
We can use the @Override annotation before the method
todeclare the overriding.EXAMPLE: get method WebDriver driver =newChromeDriver();
driver.get("URL")==>opensthe url from chrome
// Java program to demonstrate working of method// overloading in JavapublicclassSum{// Overloaded sum(). This sum takes two int parameterspublicintsum(int x,int y){return(x + y);}// Overloaded sum(). This sum takes three int parameterspublicintsum(int x,int y,int z){return(x + y + z);}// Overloaded sum(). This sum takes two double// parameterspublicdoublesum(double x,double y){return(x + y);}// Driver codepublicstaticvoidmain(String args[]){Sum s =newSum();System.out.println(s.sum(10,20));System.out.println(s.sum(10,20,30));System.out.println(s.sum(10.5,20.5));}}