Java Simple Overloading and Overriding Difference

Java Simple Overloading and Overriding Difference:

Override

Overload

1. Overriding is done in child class Overloading is done in the same class
2. Method name must be same Method name must be same
3. Argument list must be same Argument list must be different or,
Argument type must be different or,
Argument order must be different
4. Return type must be same Return type could be different
5. Access modifier can’t be less accessible Access modifier could be different
6. Resolved during Runtime Resolved during Compile time

 

An example of Overloading:

method overloading example
method overloading example

Code Method Overloading:

/**
 * Created by asif on 8/13/2015.
 */
public class Temp {

    /**
     * Adds two integers and prints their result.
     *
     * @param a first integer to add
     * @param b second integer to add
     */
    public static void addTwoNumbers(int a, int b){
        System.out.println(a + b);
    }

    /**
     * Adds two doubles and prints their result.
     *
     * @param a first double to add
     * @param b second double to add
     */
    public static void addTwoNumbers(double a, double b){
        System.out.println(a + b);
    }

    public static void main(String [] args){

        addTwoNumbers(2, 3);
        addTwoNumbers(2.2, 3.9);

    }
}

 

An example of Overriding:

method overriding example class definition
method overriding example class definition

 

method overriding example main method
method overriding example main method

Code Method Overriding Class Definition:

/**
 * Created by asif on 8/13/2015.
 */
public class MathLibrary {
    public int addIntegers(int a, int b){

        return a + b;

    }
}

class MathLibraryExtended extends MathLibrary{

    /**
     * For some reason our need is first parameter be always greater than second for the addition.
     *
     * @param a first integer to add
     * @param b second integer to add
     * @return If first integer is less than second then return -1 otherwise return their integer sum
     */
    @Override
    public int addIntegers(int a, int b){
        if(a < b){
            return -1;
        }
        return a + b;
    }

}

Code Method Overriding Main Method:

/**
 * Created by asif on 8/13/2015.
 */
public class C1 {
    public static void main(String [] args){

        MathLibraryExtended mle = new MathLibraryExtended();

        int sum = mle.addIntegers(2, 3);
        Test(sum);

        sum = mle.addIntegers(3, 2);
        Test(sum);

    }

    static void Test(int sum){
        if(sum == -1)
            System.out.println( "Please make first integer greater than second to add" );
        else
            System.out.println( sum );
    }
}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s