Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Java For Automation

  • What is Java?
  • Why to Learn Java?
  • History of Java
  • Types of Java Applications
  • Naming Conventions
  • Hello World Program
  • Internal Details of Java program
  • What is Java JDK, JRE and JVM ?
  • What is Java Comments ?
  • What is Java Variable?
  • Variable Naming convention
  • Primitive Data Types
  • Non Primitive Data Types
  • Type Casting
  • Java Arithmetic Operators
  • Relational Operators
  • What is logical operators in Java? 
  • What is Assignment Operators in Java? 
  • What is Unary Operators in Java? 
  • What is Ternary Operators in Java?
  • What is Java Control Statements?
  • What are Decision Making statements?
  • What are looping statements?
  • What are Jump statements?
  • What is OOPS concept in Java?
  • Advantages of Java OOPs
  • What is Java Inheritance?
  • What is Java Constructor?
  • What is this keyword?
  • What is Polymorphism?
  • What is super keyword ?
  • What is final keyword? 
  • What are access modifiers?
  • What is Encapsulation? 
  • What is Abstraction? 
View Categories
  • Home
  • Tutorials
  • Java
  • Java For Automation
  • What is Polymorphism?

What is Polymorphism?

What is Polymorphism?

Polymorphism in Java is a core concept of Object-Oriented Programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. It enables one interface to be used for a general class of actions, with the specific action determined by the exact nature of the situation.

Types of Polymorphism #

  1. Compile-Time Polymorphism (Method Overloading)
  2. Run-Time Polymorphism (Method Overriding)

1. Compile-Time Polymorphism (Method Overloading) #

Definition: Compile-time polymorphism, also known as method overloading, occurs when multiple methods in the same class have the same name but different parameter lists. The method that is called is determined at compile time based on the method signature.

Example:

class Printer {

    // Method to print an integer

    void print(int a) {

        System.out.println("Integer: " + a);

    }

    // Method to print a double

    void print(double a) {

        System.out.println("Double: " + a);

    }

    // Method to print a string

    void print(String a) {

        System.out.println("String: " + a);

    }

}

public class Main {

    public static void main(String[] args) {

        Printer p = new Printer();

        p.print(10);        // Calls print(int)

        p.print(10.5);      // Calls print(double)

        p.print("Hello");  // Calls print(String)

    }

}

Output:
Integer: 10
Double : Double
Double : Hello

In this example, the print method is overloaded with different parameter types. The appropriate method is selected based on the argument passed to it.

2. Run-Time Polymorphism (Method Overriding) #

Definition: Run-time polymorphism, also known as method overriding, occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The method to be executed is determined at runtime based on the object type.

Example:

// Superclass

class Animal {

    void makeSound() {

        System.out.println("Animal makes a sound");

    }

}

// Subclass 1

class Dog extends Animal {

    @Override

    void makeSound() {

        System.out.println("Dog barks");

    }

}

// Subclass 2

class Cat extends Animal {

    @Override

    void makeSound() {

        System.out.println("Cat meows");

    }

}

public class Main {

    public static void main(String[] args) {

        Animal myAnimal;

        myAnimal = new Dog();
        myAnimal.makeSound(); 

        myAnimal = new Cat();
        myAnimal.makeSound();

    }

}

Output:
Dog barks
Cat meows

In this example, makeSound is overridden in both Dog and Cat subclasses. At runtime, the method of the actual object type (either Dog or Cat) is called, even though the reference is of type Animal.

Summary #

  • Compile-Time Polymorphism (Method Overloading): Achieved by defining multiple methods with the same name but different parameters within the same class. The method to be invoked is determined at compile time.
  • Run-Time Polymorphism (Method Overriding): Achieved by overriding a superclass method in a subclass. The method to be invoked is determined at runtime based on the actual object type.

Polymorphism is a powerful feature that enhances flexibility and maintainability in code, allowing for more generic and reusable code.

Java
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
What is this keyword?What is super keyword ?
Table of Contents
  • Types of Polymorphism
  • 1. Compile-Time Polymorphism (Method Overloading)
  • 2. Run-Time Polymorphism (Method Overriding)
  • Summary
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105