java - Incorporating Polymorphism -


i'm new java , need employ polymorphism following lines:

horse.feed(); horse.wash(); horse.exercise(); 

how do this?

these first lines of code in exercise:

public class main extends object {      public static void main(string [] args) {         horse horse = new horse();     } } 

answering question comment: "could write out how should can better understand it."

let's create interface animal

public interface animal {     public void feed();     public void wash();     public void exercise(); } 

and class horse:

public class horse implements animal {     @override     public void feed() {         // feed horse     }      @override     public void wash() {         // wash horse     }      @override     public void exercise() {         // exercise horse     } } 

now in main method, can create horse animal , call methods:

animal horse = new horse(); horse.wash(); // etcetera 

now if make class dog, implements animal, make list of animals , add horses , dogs 1 list!


Comments