java dynamic polymorphism

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*");
}
}
//*

java dynamic polymorphism