Dynamic Polymorphism in Java

Introduction to Dynamic Polymorphism in Java

Dynamic polymorphism in Java is a feature that allows a method call to be resolved at runtime, rather than compile time. This is achieved through method overriding, which allows a subclass to provide its own implementation of a method that is already defined in its superclass.

For example, the following code shows a simple example of dynamic polymorphism:

public class Animal {
  public void eat() {
    System.out.println("The animal is eating.");
  }
}

public class Dog extends Animal {
  @Override
  public void eat() {
    System.out.println("The dog is eating.");
  }
}

In this example, the `Dog` class overrides the `eat()` method that is defined in the `Animal` class. This means that when you call the `eat()` method on a `Dog` object, the `eat()` method that is defined in the `Dog` class will be called instead of the `eat()` method that is defined in the `Animal` class.

Dynamic polymorphism is a powerful feature that allows you to write more flexible and reusable code. For example, if you have a list of `Animal` objects, you can call the `eat()` method on each object in the list, and the appropriate `eat()` method will be called for each object, depending on its type.

Here are some of the benefits of using dynamic polymorphism in Java:

  • Increased flexibility: Dynamic polymorphism allows you to write more flexible code that can adapt to different situations. For example, you can use dynamic polymorphism to implement different algorithms for different types of data.
  • Increased reusability: Dynamic polymorphism allows you to write more reusable code by allowing you to define methods that can be used for different types of data. For example, you can define a `print()` method that can print any type of object.
  • Reduced errors: Dynamic polymorphism can help to reduce errors in your code by preventing you from calling methods that do not exist on a particular type of object.

Overall, dynamic polymorphism is a powerful tool that can help you to write more flexible, reusable, and reliable Java code.

Here are some tips for using dynamic polymorphism effectively:

  • Use method overriding to implement dynamic polymorphism.
  • Use the `super` keyword to call the overridden method in the superclass.
  • Be careful not to overuse dynamic polymorphism, as this can make your code difficult to read and maintain.
  • Document the reasons for using dynamic polymorphism in your code.

If you are working with Java, I encourage you to learn more about dynamic polymorphism and how to use it effectively.

Exercises

Demonstration of upcasting concept using code


  //upcasting example 
  package javaprogrammingdemo;
  public class javalabclass {
  
    public static void main(String[] args) {
    
    superclass s = new subclass(); //this is upcasting
    s.superclass_method();
    s.display();
    }	
  }
  
  class superclass
  {
    public void display() {
      System.out.println("Hello from superclass");
    }
    
    public void superclass_method()
    {
      System.out.println("I am only superclass method");
    }
  }
  
  class subclass extends superclass
  {
    public void display() {
      System.out.println("Hello from subclass");
    }
    
    public void subclass_method()
    {
      System.out.println("I am only subclass method");
    }
  }   

Write a Java program that uses Dynamic polymorphism to create an array of objects that inherits the shape class. Invoke the overridden display methods for all objects in the array.


  package javaprogrammingdemo;
  public class javalabclass {
  
  public static void main(String[] args) {
  
    shape s[] = new shape[5];
    s[0]=new circle(); //upcasting
    s[1]=new rectangle();
    s[2]=new square();
    s[3]=new hexagon();
    s[4]=new polygon();
    for(int i=0;i<s.length;i++)
    {
      s[i].display_shape();
    }
  }
  
  }
  
  class shape
  {
    public void display_shape()
    {
      System.out.println("this is a shape");
    }
  }
  
  class circle extends shape
  {
    public void display_shape()
    {
      System.out.println("this is a circle");
    }
  }
  
  class square extends shape
  {
    public void display_shape()
    {
      System.out.println("this is a square");
    }
  }
  
  class rectangle extends shape
  {
    public void display_shape()
    {
      System.out.println("this is a rectangle");
    }
  }
  
  class hexagon extends shape
  {
    public void display_shape()
    {
      System.out.println("this is a hexagon");
    }
  }
  
  class polygon extends shape
  {
    public void display_shape()
    {
      System.out.println("this is a polygon");
    }
  }