There is nothing to note about polymorphism. The keyword worth noting is instanceof. For the explanation, "it can determine whether an object is an instance or a subclass of a class"
The Code is as follows:
Package D10; public class animaltest {public static void main (string [] ARGs ){
// The method stub automatically generated by todo
Animal a1 = new CAT (); // The new object is referenced by animal Class A1.
Animal a2 = new dog ();
Animalshout (A1 );
// Animalshout (A2 );
}
Public static void animalshout (animal SH) {// Implementation Method of Parameter validation using Polymorphism
Sh. Shout ();
Cat cat = (CAT) Sh;
Cat. Sleep ();
}
}/* Partition ------ split */package D10; Class cat implements animal {
Public void shout () {// What if public is removed?
System. Out. println ("meow ~ ");
}
Public void sleep () {// What if public is removed?
System. Out. println ("sleep ~ ");
}
}/* Partition ------ split */package D10; Class dog implements animal {
Public void shout (){
System. Out. println ("Wang ~ ");
}
}/* Partition ------ split */package D10; interface animal {
Void shout ();
} Implementation:
Package D10; public class animaltest {public static void main (string [] ARGs ){
// The method stub automatically generated by todo
Animal a1 = new CAT (); // The new object is referenced by animal Class A1.
Animal a2 = new dog ();
Animalshout (A1 );
Animalshout (A2 );
}
Public static void animalshout (animal SH) {// Implementation Method of Parameter validation using Polymorphism
If (SH instanceof cat ){
Sh. Shout ();
Cat cat = (CAT) Sh;
Cat. Sleep ();
} Else {
Sh. Shout ();
}
}
}
4.4 polymorphic