Polymorphism in Java and Difference between method overloading and method overriding
Skip to main content

Polymorphism in Java and Difference between method overloading and method overriding

 Polymorphism in Java and Difference between method overloading and method overriding, polymorphism, polymorphism java, polymorphism oop, polymorphism programming, polymorphism in python, polymorphism definition, polymorphism computer science, polymorphism example, polymorphism example in java, polymorphism function, polymorphism in java, difference between method overloading and method overriding in java, difference between method overloading and method overriding




 

Java Tutorials with GKR

Introduction:-

 Polymorphism in Java and Difference between method overloading and method overriding in this tutorial is explain about what is polymorphism in Java and also explain about method Overloading and Method Overriding with Example of Java Programming. In this blog we also describe about the difference between method Overloading and Method Overriding in Java.


Polymorphism in Java:- Polymorphism means "more form" this is allow us to perform a single task in different ways. For the polymorphism we use method overloading and method overriding.

Method Overloading :- When defined two or more method within the same class that's method name is same but their parameters are different. That case these methods is overloaded and this process is called Method OverloadingMethod overloading is support compile time polymorphismOverloaded methods have different return type.


Most Read:- Features of Java


Example:-

 

class OverloadingExample{

        display(int a){

          System.out.println("Display first method");

        }

       display(int a, int b){

          System.out.println("Display second method");

        }

display(int a, double b){

          System.out.println("Display third method");

        }

}

class Main {

         public static void main(String [] args){

             OverloadingExample example = new OverloadingExample();

          example.display(10);

           example.display(10, 20);

           example.display(10, 15.56);

       }

}

Output:- Display first method

Display second method

Display third method

 

Most Read:- Data types in Java


Example:- In this example we using inheritance. When we create a OverloadingExample class and we create a display function after that we create another class second and there is create another display name function with same name and different signature of super class. class name second is extended by OverloadingExample. 

 

Example:-

class OverloadingExample{

        display(int a){

          System.out.println("Display first method");

        }

}

class Second extends OverloadingExample {

display(int a, int b){

          System.out.println("Display second method");

        }

}

class Main {

         public static void main(String [] args){

           Second example = new Second();

           example.display(10);

           example.display(10, 20);

       }

}

Output:- Display first method

Display second method

 

Most Read:- Program to print first five characters using recursive method in Java


Method Overriding:- When a method in a sub class has the same name and signature as a method of its super class. Then the method of sub class is overriden the method super class, that process is called Method OverridingMethod overriding support run time polymorphism.

 

Example:- We create a two display name method in super class and sub class. Sub class access all the properties and behaviours of super class so when class the method display in Main class its replace the super class method name display with the sub class method display. Overriding is used when we require to replace with new features in program on old function

 

Example:-

class OverloadingExample{

        display(){

          System.out.println("Display OverloadingExample class ");

        }

}

class Second extends OverloadingExample {

display(){

          System.out.println("Display second class");

        }

}

class Main {

         public static void main(String [] args){

           Second example = new Second();

           example.display();

         

       }

}

Output:- Display second method


 Most Read:- Best top 10 Introduction of Java interview questions in 2022


Difference between Method Overloading and method overriding ?

 

Method Overloading

Method Overriding

1. In this methods name is same but signature are different

1. In this methods name and signatures both same

2. that is support compile line polymorphism

2. It is support run time polymorphism

3. In this methods return types is may or may not equal

3. In this methods return type must be same

4. In this may or may not require to use of inheritance

4. In this require inheritance.

5. In this use static keyword

5. In this we cannot use static keyword

6. In this we use access modifiers

6. In this we cannot use access modifiers

Post a Comment

0 Comments