+1 vote
in JAVA by
What is Method Overriding? Why is it used in Java?

2 Answers

0 votes
by
What is Method overloading? Why is it used in Java?

If multiple functions in a class have the same name but different function definitions it is called method overloading.

It is used to make a java function serve multiple purposes making the code cleaner and API less complex.

Example:

println() prints any data type passed to it as a string.

public class Add_Overload {

    void add(int x, int y){

        System.out.println(x+y);

    }

    void add(double x, double y){

        System.out.println(x+y);

    }

    void add(double x, int y){

        System.out.println(x+y);

    }

    public static void main(String args[]){

        Add_Overload a= new Add_Overload();

        a.add(10,20);

        a.add(20.11,11.22);

        a.add(20.11,2);

    }
0 votes
by

Method overriding in Java is a mechanism where a subclass provides a new implementation for a method that is already defined in its superclass. The new implementation overrides the implementation in the superclass.

Related questions

0 votes
asked May 25, 2020 in JAVA by Robindeniel
+1 vote
asked May 24, 2019 in JAVA by rajeshsharma
...