How to Create Polymorphism in JAVA

 

In this article, you will learn How to Create Polymorphism in JAVA.

Example:-

class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Pig extends Animal {
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

class Dog extends Animal {
  public void animalSound() {
    System.out.println("The dog says: bow wow");
  }
}

class MyMainClass {
  public static void main(String[] args) {
    Animal myAnimal = new Animal();
    Animal myPig = new Pig();
    Animal myDog = new Dog();
        
    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }
}

Comments

Popular posts from this blog

Student Result Management System

What Is The Job Role Of A Customer Support Executive?

TOP RECRUITMENT COMPANIES IN INDIA