override method java code example
Example 1: what's method overriding
Overriding means same method name and same parameter,
occur in different class that has inheritance relationship.
we use method overriding to implement specific functionality to the method.
Example: get method
WebDriver driver = new ChromeDriver();
driver.get("URL") ==> opens the url from chrome
WebDriver driver = new FireFoxDriver();
driver.get("URL") ==> opens the url from Firefox
we can only override instance methods and method override
takes place in sub class.
instance method that we are going to override cannot be private and final
Example 2: overriding in java
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String args[]) {
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move(); // runs the method in Animal class
b.move(); // runs the method in Dog class
}
}
Example 3: overriding in java
Overriding means same method name and same parameter,
occur in different class that has
inheritance relationship.
we use method overriding to implement
specific functionality to the method.
Examples are get and navigate methods
of different drivers in Selenium .
Example: get method
WebDriver driver = new ChromeDriver();
driver.get("URL") ==> opens the url from chrome
WebDriver driver = new FireFoxDriver();
driver.get("URL") ==> opens the url from Firefox
we can only override instance methods and method override
takes place in sub class.
instance method that we are going to override cannot be private and final
Example: get method
WebDriver driver = new ChromeDriver();
driver.get("URL") ==> opens the url from chrome
WebDriver driver = new FireFoxDriver();
driver.get("URL") ==> opens the url from Firefox
we can only override instance methods and method override
takes place in sub class.
instance method that we are going to
override cannot be private and final
Example 4: method overriding java
Method Overloading:
Access modifier can be same or different,
Return-Type can be same or different,
Parameters MUST be different, Method name MUST be same,
any method can be overloaded
Method Overriding:
After a method is inherited it is possible to change
the implantation of the method in the child class.
This concept is called overriding.
Method name, Parameter, and Return-Type MUST 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
to declare the overriding.
EXAMPLE: get method WebDriver driver = new ChromeDriver();
driver.get("URL") ==> opens the url from chrome
Example 5: method override
using method-override in your index.js
1. npm install method-override --save
2. var methodOverride = require("method-override");
3. app.use(methodOverride("_method"));
//'_method' is what methodOverride will look for
method override in your UPDATE route:
* <form action = "/campgrounds/<%=campground._id%>/edit?_method=PUT"
method = "POST">
and EDIT routes: