Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Polymorphism: Overloading and Overriding

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 357
    Comment on it

    Polymorphism:

    Polymorphism is one of the OOPs pillars. Polymorphism is the ability of a method to behave differently as per the object. Polymorphism allows programmer to declare a method and use it differently based on the instance.

    In Java polymorphism is implemented by method overloading and method overriding.

    Method overloading:

    In overloading we can have two or more methods with same name but there number of arguments and parameters differ.

    Example:

    class MethLoad
    {
        void show (int a)
        {
           System.out.println ("a: " + a);
        }
        void show (int a, int b)
        {
           System.out.println ("a and b: " + a + "," + b);
        }
        double show(double a,double b) {
           System.out.println("double a and b: " + a + "," + b);
    
        }
    }
    class Overloading
    {
        public static void main (String  [] args)
        {
            MethLoad obj = new MethLoad();
    
            obj .show(10);
            obj .show(10, 20);
            obj .show(5.5,6.3);
    
        }
    }
    

    In the above example there is a class MethLoad in this show method has been overloaded. First it takes one integer type argument then it is taking two integer type arguments and last it is taking two double arguments.

    Method Overriding:

    Method overriding can be defined as when the sub class has the same method as base class but the implementation is different in sub class.

    Example:

    class Over{  
    void show()
                    {
         System.out.println("Good morning");}  
                   }  
    
    class SubMain extends Over{  
        void show()
          {
               System.out.println("Good afternoon");
           }  
    
    public static void main(String args[]){  
    SubMain obj = new SubMain();  
    obj.show();  
    }  
    }
    

    As in the above example there is a base class Over and it is having a method show and the over class is inherited by SubMain class and it is overriding show() method of Over class.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: