Polymorphic advantages and disadvantages

advantage:

  Improve the maintenance of the code (inherited guarantee)

  Improved scalability (polymorphic guarantees) code

Disadvantages:

  You can not use function-specific subclasses

. 1  class duotaidemo {
 2      public  static  void main (String [] args) {
 . 3          Fu = F new new Zi ();     // upcast 
. 4          f.show ();
 . 5          // f.method (); 
. 6          Zi = Z ( zi) F;    // downward transition 
. 7          z.method ();
 . 8      }
 . 9      public  static  class Fu {
 10          public  void Show () {
 . 11              System.out.println ( "Show FU" );
 12 is          }
 13 is     }
14     
15     public static class Zi extends Fu{
16         public void show() {
17             System.out.println("show zi");
18         }
19         
20         public void method() {
21             System.out.println("method zi");
22         }
23     }
24 }

operation result:

show zi
method zi

If you want to use specific features subclass put the parent class references cited strong into subclasses.

 

Upcast:
            Fu = f new new Zi ();
        downcast:
            Zi = Z (Zi) f; f // the requirement must be able to convert the Zi.

Guess you like

Origin www.cnblogs.com/lsymove/p/11222460.html