Sunday, September 3, 2023

Difference between Over loading and Overriding

 

 

METHOD

OVERLOADING

METHOD

OVERRIDING

*      It occurs within the class.

*      It is performed in two classes with inheritance relationships.

*      Method overloading may or may not require inheritance.

*      Method overriding always needs inheritance.

*      In method overloading, methods must have the same name and different signatures.

*   In method overriding, methods must have the same name and same signature.

*      Poor Performance due to compile time polymorphism.

*   It gives better performance. The reason behind this is that the binding of overridden methods is being done at runtime.

*   The argument list should be different while doing method overloading.

*   The argument list should be the same in method overriding.

*   The overloaded and overloading methods must be in the same class

*   The overriding method must take the same number and type of parameters as the overridden method – otherwise, you would just be overloading the method.


*   Method overloading is defining several methods in the same class, that accept different numbers and types of parameters

 

*   Method overriding is when a child class redefines the same method as a parent class, with the same parameters.

 

*      Method overloading is a compile-time polymorphism.

*      Method overriding is a run-time polymorphism.

*   Method overloading is used to increase the readability of the program.

*   Method overriding is used to provide the specific implementation of the method that is already provided by its super class

*   The return type can be freely modified.

 

*   The return type must not change

*   Static method binding

*   Dynamic method binding

*   Exceptions can change

*    Exception can be reduced or eliminated

*   Overloading lets you define a similar operation in different ways for different data

*   Overriding lets you define a similar operation in different ways for different object types

EXAMPLE-

// Java program to demonstrate working of method

// overloading in Java

  

publicclassSum {

    publicintsum(intx, inty) { return(x + y); }

  

publicintsum(intx, inty, intz)

    {

        return(x + y + z);

    }

    publicdoublesum(doublex, doubley)

    {

        return(x + y);

    }

publicstaticvoidmain(Stringar[])

    {

        Sum s = newSum();

System.out.println(s.sum(10, 20));

        System.out.println(s.sum(10, 20, 30));

        System.out.println(s.sum(10.5, 20.5));

    }

}

 

EXAMPLE-

// A Simple Java program to demonstrate

// method overriding in java

 

classParent {

    voidshow() { System.out.println("Parent's show()"); }

}

 

classChild extendsParent {

    voidshow()

    {

System.out.println("Child's show()");

    }

}

 classMain {

    publicstaticvoidmain(String[] args)

    {

  Parent obj1 = newParent();

        obj1.show();

Parent obj2 = newChild();

        obj2.show();

    }

}

 

 


1 comment:

Machine Learning Course Assignments 2024(Dec)

 Machine Learning Course Assignments 2024 1)   Explain the concept of "generalization" in machine learning. Why is it a central go...