What is Polymorphism?

What is Polymorphism?

Asked on November 13, 2018 in Java.
Add Comment


  • 2 Answer(s)

    Polymorphism is the capability of a method to do different things based on the object that it is acting upon. In other words, polymorphism allows you define one interface and have multiple implementations. As we have seen in the above example that we have defined the method sound() and have the multiple implementations of it in the different-2 sub classes.
    Which sound() method will be called is determined at runtime so the example we gave above is a runtime polymorphism example.

    Types of polymorphism and method overloading & overriding are covered in the separate tutorials. You can refer them here:
    1. Method Overloading in Java – This is an example of compile time (or static polymorphism)
    2. Method Overriding in Java – This is an example of runtime time (or dynamic polymorphism)
    3. Types of Polymorphism – Runtime and compile time – This is our next tutorial where we have covered the types of polymorphism in detail. I would recommend you to go though method overloading and overriding before going though this topic.

    Lets write down the complete code of it:

     Polymorphism in Java

    Runtime Polymorphism example:
    Animal.java

    public class Animal{
       public void sound(){
          System.out.println("Animal is making a sound");   
       }
    }

    Horse.java

    class Horse extends Animal{
        @Override
        public void sound(){
            System.out.println("Neigh");
        }
        public static void main(String args[]){
        	Animal obj = new Horse();
        	obj.sound();
        }
    }
    Answered on January 18, 2019.
    Add Comment

    Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

    Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.

    It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared, the type of a reference variable cannot be changed.

    The reference variable can be reassigned to other objects provided that it is not declared final. The type of the reference variable would determine the methods that it can invoke on the object.

    A reference variable can refer to any object of its declared type or any subtype of its declared type. A reference variable can be declared as a class or interface type.

    Example

    Let us look at an example.

    public interface Vegetarian{}
    public class Animal{}
    public class Deer extends Animal implements Vegetarian{}

    Now, the Deer class is considered to be polymorphic since this has multiple inheritance. Following are true for the above examples −

    • A Deer IS-A Animal
    • A Deer IS-A Vegetarian
    • A Deer IS-A Deer
    • A Deer IS-A Object

    When we apply the reference variable facts to a Deer object reference, the following declarations are legal −

    Example

    Deer d = new Deer();
    Animal a = d;
    Vegetarian v = d;
    Object o = d; 
    etc.......
    Answered on January 18, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.