Java method overloading and overriding method

Java overloading and method overriding method

Method overloading in Java

Method overloading: When a method in a class of Java having the same method name with different arguments or signatures is called method overloading.Java method overloading is also known as Static Polymorphism. Method overloading enhances readability.

Why method overloading?


Suppose we need to perform a multiplication function, and the user can input one or two or three or more arguments, and it is also impossible to declare, assign or define new methods for each and every number of user inputs as it will be confusing and time-consuming. Here we can use method overloading for this problem.

Example: 
import java.io.*;
public class OverLoadFunction
{
    int a, b;
    char a1, b1;
    String a2, b2;
    void compare(int x, int y)
    {
        a= x;
        b= y;
        if(a>b)
         System.out.println("greatest="+a);
        else
         System.out.println("greatest="+b);
    }
    void compare(char x, char y)
    {
        a1= x;
        b1= y;
        if(a1>b1)
         System.out.println("Higher value="+a1);
        else
         System.out.println("Higher Value="+b1);
    }
    void compare(String x, String y)
    {
        a2= x;
        b2= y;
        int n, m;
        n= a2.length();
        m= b2.length();
        if(n>m)
         System.out.println("Longer String="+a2);
        else
         System.out.println("Longer String="+b2);
    }
    public static void main(String args[])throws IOException
    {
        InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(in);
        int a, b;        // Local Variables
        char a1, b1;
        String a2, b2;
        OverLoadFunction ob=new OverLoadFunction();
        System.out.println("Enter two number");
        a= Integer.parseInt(br.readLine());
        b= Integer.parseInt(br.readLine());
        ob.compare(a,b);
        System.out.println("Enter two Character");
        a1= (char)br.read();
        b1= (char)br.read();
        ob.compare(a1,b1);
        System.out.println("Enter two String");
        a2= br.readLine();
        b2= br.readLine();
        ob.compare(a2,b2);
    }
        
}

Method overriding in Java

Method overriding: Overridden the function is the same name method both in the Base class and in the Derive class. The same name function and the same signature of the base class are overwritten in the drive class. It is must be an IS-A relation. It is used for Runtime polymorphism. A static method can not override in Java. 
Example:
//First Class
public class ClassA
{
   void display()
   {
       System.out.println("I am from ClassA");
    }
}
//Second class
public class ClassB extends ClassA
{
    void display()
   {
       System.out.println("I am from ClassB");
   }
   public static void main(String args[])
   {
       ClassB ob=new ClassB();
       ob.display();
    }
        
}

More Java program
Method Overloading

SHARE THIS
Previous Post
Next Post