Implementation of polymorphism in Java: interface implementation, inheriting the parent class for method overrides, and method overloading in the same class.
Look at the code:
Package Com.company; Public classMain { Public Static voidMain (string[] args) {A A1=NewA (); A A2=NewB (); b b=NewB (); C C=NewC (); D d=NewD (); System. out. println (a1.show (b)); System. out. println (A1.show (c)); System. out. println (A1.show (d)); System. out. println (a2.show (b)); System. out. println (A2.show (c)); System. out. println (A2.show (d)); System. out. println (b.show (b)); System. out. println (B.show (c)); System. out. println (B.show (d)); }}classA { PublicString Show (D obj) {return("A and D"); } PublicString Show (A obj) {return("A and a"); }}classB extends A { PublicString Show (B obj) {return("B and B"); } PublicString Show (A obj) {return("B and A"); }}classC extends B {}classD extends B {}
Http://www.cnblogs.com/jack204/archive/2012/10/29/2745150.html
Java polymorphic Code