Polymorphism in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vikas Rana
    New Member
    • Jan 2016
    • 3

    Polymorphism in Java

    What is the difference between static and dynamic Polymorphism in java?
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Vikas Rana, is this a homework question?
    Such threads are usually closed when found.
    -z

    Comment

    • Ishan Shah
      New Member
      • Jan 2020
      • 47

      #3
      Static polymorphism is a type of polymorphism, which collects data to call a method during compilation. Whereas dynamic polymorphism is a type of polymorphism that collects information for calling a method at the time of execution. So that's the main difference between static and dynamic polymorphism.

      Comment

      • vipulguptaseo
        New Member
        • Feb 2023
        • 22

        #4
        Polymorphism is a fundamental concept in object-oriented programming (OOP) and refers to the ability of an object to take on many forms. In Java, polymorphism is achieved through method overloading and method overriding.

        Method overloading allows multiple methods to have the same name but with different parameters. When a method is called, Java determines which version of the process to execute based on the number and types of arguments passed to it.

        Here is an example of method overloading in Java:
        public class MyClass {

        public void myMethod(int num) {

        System.out.prin tln("This is an integer: " + num);

        }



        public void myMethod(String str) {

        System.out.prin tln("This is a string: " + str);

        }



        public void myMethod(double dbl) {

        System.out.prin tln("This is a double: " + dbl);

        }

        }

        In this example, the `myMethod` method is overloaded three times with different parameter types. When called, the appropriate version of the method is executed based on the argument passed.

        Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. This allows the subclass to inherit the methods of the superclass while still being able to customize their behavior.

        Comment

        • Nikhilesh10
          New Member
          • Feb 2023
          • 14

          #5
          Static polymorphism is a type of polymorphism, which collects data to call a method during compilation.
          Whereas dynamic polymorphism is a type of polymorphism that collects information for calling a method at the time of execution.
          So that's the main difference between static and dynamic polymorphism.

          Comment

          Working...