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

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

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

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




Java

Related
android pass object to activity Code Example android pass object to activity Code Example
how to convert a string to char in java Code Example how to convert a string to char in java Code Example
android studio define array Code Example android studio define array Code Example
spring boot default value false Code Example spring boot default value false Code Example
how to get current date in java Code Example how to get current date in java Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7