Java dynamic polymorphism ✨

Published: 29 June 2020
on channel: Bro Code
87,361
4k

Java dynamic runtime polymorphism tutorial explained

#java #dynamic #polymorphism #runtime

//*************************************************
import java.util.Scanner;
public class Main {

public static void main(String[] args) {

//Dynamic Polymorphism

Scanner scanner = new Scanner(System.in);
Animal animal;

System.out.println("What animal do you want?");
System.out.print("(1=dog) or (2=cat): ");
int choice = scanner.nextInt();

if(choice==1) {
animal = new Dog();
animal.speak();
}
else if(choice==2) {
animal = new Cat();
animal.speak();
}
else {
animal = new Animal();
System.out.println("That choice was invalid");
animal.speak();
}
}
}
//*************************************************
public class Animal {

public void speak() {
System.out.println("animal goes *brrrr*");
}
}
//*************************************************
public class Dog extends Animal{

@Override
public void speak() {
System.out.println("dog goes *bark*");
}
}
//*************************************************
public class Cat extends Animal{

@Override
public void speak() {
System.out.println("cat goes *meow*");
}
}
//*************************************************


On this page of the site you can watch the video online Java dynamic polymorphism ✨ with a duration of 08 minute 52 second in good quality, which was uploaded by the user Bro Code 29 June 2020, share the link with friends and acquaintances, this video has already been watched 87,361 times on youtube and it was liked by 4 thousand viewers. Enjoy your viewing!