1Method overloading is providing two separate methods in a class with the same name but different arguments, while the method return type may or may not be different, which allows us to reuse the same method name.
2
3Method overriding means defining a method in a child class that is already defined in the parent class with the same method signature, same name, arguments, and return type
1Method Overloading:
2Access modifier can be same or different,
3Return-Type can be same or different,
4Parameters MUST be different, Method name MUST be same,
5any method can be overloaded
6
7Method Overriding:
8After a method is inherited it is possible to change
9the implantation of the method in the child class.
10This concept is called overriding.
11Method name, Parameter, and Return-Type MUST be same
12access modifier MUST be same or more visible,
13MUST happen in the sub class,
14ONLY the instance methods can be overridden
15@Override annotation MUST be applicable.
16Static and Constructor cannot be override.
17We can use the @Override annotation before the method
18to declare the overriding.
19EXAMPLE: get method WebDriver driver = new ChromeDriver();
20driver.get("URL") ==> opens the url from chrome
21
1Overriding means same method name and same parameter,
2occur in different class that has
3inheritance relationship.
4we use method overriding to implement
5specific functionality to the method.
6
7Examples are get and navigate methods
8of different drivers in Selenium .
9
10Example: get method
11WebDriver driver = new ChromeDriver();
12driver.get("URL") ==> opens the url from chrome
13
14WebDriver driver = new FireFoxDriver();
15driver.get("URL") ==> opens the url from Firefox
16
17we can only override instance methods and method override
18takes place in sub class.
19instance method that we are going to override cannot be private and final
20Example: get method
21WebDriver driver = new ChromeDriver();
22driver.get("URL") ==> opens the url from chrome
23
24WebDriver driver = new FireFoxDriver();
25driver.get("URL") ==> opens the url from Firefox
26
27we can only override instance methods and method override
28takes place in sub class.
29instance method that we are going to
30override cannot be private and final
1Overriding means same method name and same parameter,
2occur in different class that has
3inheritance relationship.
4we use method overriding to implement
5specific functionality to the method.
1Method overloading is providing two separate methods in a class with the same name but different arguments, while the method return type may or may not be different, which allows us to reuse the same method name.
2
3Method overriding means defining a method in a child class that is already defined in the parent class with the same method signature, same name, arguments, and return type
4
1class Animal {
2 public void move() {
3 System.out.println("Animals can move");
4 }
5}
6
7class Dog extends Animal {
8 public void move() {
9 System.out.println("Dogs can walk and run");
10 }
11}
12
13public class TestDog {
14
15 public static void main(String args[]) {
16 Animal a = new Animal(); // Animal reference and object
17 Animal b = new Dog(); // Animal reference but Dog object
18
19 a.move(); // runs the method in Animal class
20 b.move(); // runs the method in Dog class
21 }
22}