Java Foundation (polymorphism)

  Polymorphism is one of the three basic features of Java programming. It is to do business through separation operation and the actual operation of abstraction, polymorphism is what to do and how to do it by separating (hide the actual implementation only need to know the base class use). Doing so can improve the organizational structure of the code to improve readability.
  The main multi-state:
  through inheritance, or to implement the abstract class level relationship. Such geometry is the geometry triangle, a square geometry;
turn may be triangular geometry may be other geometric shapes. Is a collection of triangles and squares pattern,
which is their common feature, it can be an interface or abstract class as the base class.
  How to improve the organizational structure of the code to improve readability?
  It is possible to eliminate the coupling between the type of the line is the same geometry can be triangular or other geometric shapes; be of the type which allows the object of his own, which may be substantially the base class for processing.
  Upward transition:
  with a base class to accept the type of reference called the transition from class up, it is safe. Because the sub-class is a concrete implementation of the parent class, so that the geometry of this triangle is not a problem.
  Upward transition there is a problem, since the unique features of the class can not be used by the base class, the base class is not defined because this function.
  Advantages in the development of the base class you just need to know what to do, without knowing how he was doing.
  Downcast:
  the abstract concrete realization of turn, there are risks. On the line geometry is a triangle? He may not necessarily be other geometric shapes, so the downward transition must be to determine the specific type of downcast. Will complain if you do not know the specific implementation class, running

Guess you like

Origin www.cnblogs.com/liaobudao/p/11221713.html