27 July 2023

Polymorphism In Java - learngreen.net

 Polymorphism In Java

  Polymorphism is an important concept in object-oriented programming. It's from the four most important concepts of object-oriented programming which are encapsulation, inheritance and abstraction, and polymorphism. It treats objects of different classes common as objects of superclass which helps in code reusability. Polymorphism gets into action through method overriding and method overloading. We have already discussed about method overloading and method overriding in our previous blogs.

Let's recall method overloading and method Overriding:-


Method Overriding:-

Method overriding permits the subclass to implement the method from the superclass which has the same name, return type, and parameters. This allows the subclass to replace or extend the behavior of the superclass method.






  //Example Method Overriding
  package Java;
  /* Base class representing a
   * TwoWheeler with a method company() 
   */
  class TwoWheeler {
  void company() {
  System.out.println("Honda");
    }
  }
  /* Subclass of TwoWheeler representing
   *  a vehicle with a method company()
   */
  class vehicle extends TwoWheeler {
  /* Method overriding: Custom implementation
   * of company() for the vehicle subclass
   */
   void company() {
   System.out.println("BMW");
     }
  }

  /* Another subclass of TwoWheeler
   * representing an automobile with
   * a method company()
   */
  class automobile extends TwoWheeler {
  /* Method overriding: Custom implementation
   * of company() for the automobile subclass
  */
  void company() {
  System.out.println("Bajaj");
     }
  }

  /* Main class MDOR to
   * test the vehicle hierarchy
   */
  public class MDOR {
  public static void main(String[] args) {
  TwoWheeler obj = new TwoWheeler();
  obj.company(); // Output: Honda
  vehicle obj1 = new vehicle();
  obj1.company(); // Output: BMW
  automobile obj2 = new automobile();
  obj2.company(); // Output: Bajaj
        }
    }
 

 

 Method Overloading:-

Method overloading in Java possesses multiple methods having the same name but different parameters. These methods can have different numbers or different parameter types.


 package Java;
  public class MDOL {
  // Method to divide two integers	 
  int divide(int x, int y){
  return x/y;
   }	
  /*  Method overloading:- 
   *  Method to divide two doubles
   */
  double divide( double x, double y) {
  return x/y;	 
  }
  public static void main(String[] args) {
  MDOL obj = new MDOL();
  /* Call the int version
  * of divide method, result is 2 (8 / 4) 
  */
  System.out.println(obj.divide(8,4));
  /* Call the double version of divide method, 
   * result is 4.0 (8.0 / 2.0)
   */
  System.out.println(obj.divide(8.0, 2.0));
  //Output-> 2
  //         4.0

	}
 }

 


 //Example of Polymorphism
 package Java;
 public class PYMS {
 public static void main(String[] args) {
 //Runtime Polymorphism (Dynamic Polymorphism)
 /* Method overriding has runtime polymorphism
  * which permits objects of different classes
  * to act as objects of their common superclass
  * and enables to implement to decide the JVM
  * which specific method needs to be declared.
  * 	 
  */
  TwoWheeler obj = new TwoWheeler();
  /* Classes vehicle and automobile overriding
   * the method company() of superclass Twowheeler 
   * This is an example of Rutime polymorphism	 
   */
  vehicle obj1 = new vehicle();
  automobile obj2 = new automobile();
  obj.company();
  obj1.company();
  obj2.company();
 
  //Compile time Polymorphism(Static Polymorphism)
  /* Compile time polymorphism comes in action 
  * by method overloading.This permits different 
  * methods with same name but having different
  * parameters which are from same class.Types
  * of arguments has been provided during method
  * declaration choosing appropriate method 
  * determine by the compile time.
  */
  MDOL object = new MDOL();
  /* Two overloaded methods has been called
  * one which takes as integers and other which
  * takes as doubles Java compiler identifies
  * the correct method to be called based on
  * the data types of arguments.This is an
  * example of compile-time polymorphism. 
  */
  System.out.println(object.divide(8,4));
  System.out.println(object.divide(8.0, 2.0));
 
  /* Output->
  * Honda
  * BMW
  * Bajaj
  * 2
  * 4.0
  */
	}
  }
 

 

No comments:

Post a Comment