All about Polymorphism in Java - Selenium Tutorials Beginners

In the present day world, Java is fast becoming the most widely used programming language and many ambitious persons are learning java to get better career opportunities. Polymorphism is a term that is quite often heard in the discussions about java and you might wonder what is a polymorphism. This article deals with a polymorphism in java.

In nutshell, polymorphism is the capability of an object to take many forms. When a parent class reference is to be used to refer a child class, polymorphism will come into use. All the objects in java that have passed IS-A test are polymorphic. There are two types of polymorphism in Java and polymorphism can be performed by method overloading and overriding.

Runtime Polymorphism

When a call to an override method is resolved at runtime and not at compile time, it is called as runtime polymorphism or Dynamic Method Dispatch. A reference variable of the superclass is used in this method to call an overridden method. The method to be called will be determined based on the object referred by the reference variable.

Example for Runtime Polymorphism in Java

Here, two classes that have been created, Cars and Mercedes. Mercedes class extends cars class and override its run() method. Run method will be called by reference variable of the parent class. As it refers to the subclass objects and the parent class method is being overridden by the subclass method, the subclass method is invoked at runtime.

  1. class Cars{  
  2.  void run(){System.out.println(“running”);}  
  3. }  
  4. class Mercedes extends Cars{  
  5.  void run(){System.out.println(“running safely with 60km”);}  
  6.  
  7.  public static void main(String args[]){  
  8.    Bike b = new Mercedes();//upcasting  
  9.    b.run();  
  10.  }  
  11. }  

Compile time Polymorphism

Unlike in the case of Runtime polymorphism here, the Call to an overridden method is resolved at compile time and not at runtime.

Learning polymorphism in Java is an important aspect of Java tutorials and it is dealt in detail. The java training videos which are being provided as a part of online Java training course will cover polymorphism in detail. Trainees will get a chance to familiarize themselves with a polymorphism in java during live project training also.

The task is to identify the best Java Training course and trainer. You can find the right trainer if you follow the tips given below.

  • Visit the official website of the trainer and go through the sample videos and other study materials posted thereon. This will help you to understand the quality of the study materials.
  • Attend the free webinars and orientation sessions organized by the trainer from time to time. This will help you to understand more about the quality of the training and the approach of the trainer.
  • You can take the help of social media or the concerned open discussion forums to know the reputation enjoyed by the trainer among the job providers.

Leave a Reply

Your email address will not be published. Required fields are marked *