Java dynamic polymorphism ✨

Описание к видео 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*");
}
}
//*************************************************

Комментарии

Информация по комментариям в разработке