xxxxxxxxxx
class Animal {
public void displayInfo() {
System.out.println("I am an animal.");
}
}
class Dog extends Animal {
@Override
public void displayInfo() {
System.out.println("I am a dog.");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.displayInfo();
}
}
xxxxxxxxxx
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
xxxxxxxxxx
class BaseClass {
private void notOverride() // private methods are not overridden
{ System.out.println("From parent m1()"); }
public void methodToOverride()
{
System.out.println("From the base class");
}
}
class DerivedClass extends BaseClass {
@Override // overriding method
public void methodToOverride()
{
System.out.println("From the derived class");
}
}
class Main {
public static void main(String[] args)
{
var obj1 = new BaseClass();
obj1.methodToOverride(); // the base class method
var obj2 = new DerivedClass();
obj2.methodToOverride(); // the overriden derived class method
}
}
Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
Method overriding is used for runtime polymorphism.
xxxxxxxxxx
import java.io.*;
class Animal {
void eat()
{
System.out.println("eat() method of base class");
System.out.println("eating.");
}
}
class Dog extends Animal {
void eat()
{
System.out.println("eat() method of derived class");
System.out.println("Dog is eating.");
}
}
class MethodOverridingEx {
public static void main(String args[])
{
Dog d1 = new Dog();
Animal a1 = new Animal();
d1.eat();
a1.eat();
Animal animal = new Dog();
// eat() method of animal class is overridden by
// base class eat()
animal.eat();
}
}
xxxxxxxxxx
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
xxxxxxxxxx
public class Multi{ //Super class
public void multi(){
………………
}
}
Public class Multiplication extends Multi(){
Public void multi(){
………..
}
Public static void main(String args[]){
Multi multiplication = new Multiplication(); //Polimorphism is applied
multiplication.multi(); // It calls the Sub class add() method
}
}
xxxxxxxxxx
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.
xxxxxxxxxx
// Java program to demonstrate working of method
// overloading in Java
public class Sum {
// Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y) { return (x + y); }
// Overloaded sum(). This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
// Overloaded sum(). This sum takes two double
// parameters
public double sum(double x, double y)
{
return (x + y);
}
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
xxxxxxxxxx
static int plusMethodInt(int x, int y) {
return x + y;
}
static double plusMethodDouble(double x, double y) {
return x + y;
}
public static void main(String[] args) {
int myNum1 = plusMethodInt(8, 5);
double myNum2 = plusMethodDouble(4.3, 6.26);
System.out.println("int: " + myNum1);
System.out.println("double: " + myNum2);
}