What is polymorphism?
1: in object-oriented program design, polymorphism is the third major feature after inheritance and encapsulation.
2 :when the base class and the exported class have the same method, the method of exporting the class is first transferred.
3: The role of polymorphism is to eliminate the coupling between the base class and the derived class;
Conditions for polymorphic generation
1 An inheritance relationship exists
2 subclasses overriding the base class method
A reference to a 3base class points to a subclass
Class aa{
public void F () {
System.out.println ("A.F ()");
}
}
Class B extends aa{
public void F () {// subclass overriding base class method
System.out.println ("B.f ()");
}
}
public class c{
public static void Main (String args[]) {
Aa obji=new B ();//reference to base class to child class
OBJI.F ();
}
}
1.Replaceable (substitutability). Polymorphism is replaceable for existing code. For example, polymorphism works on the Circle Circle class and works on any other circular geometry, such as a ring.
2.Extensibility (Extensibility). Polymorphism has extensibility for code. Adding new subclasses does not affect the polymorphism, inheritance, and the operation and manipulation of other attributes of existing classes. In fact, new subclasses are more likely to get polymorphic functions. For example, it is easy to add the polymorphism of sphere class on the basis of realizing the multi-state of cone, semi-cone and hemispherical body.
3.interface (interface-ability). Polymorphism is a superclass that, by way of signature, provides a common interface to subclasses, which is implemented by subclasses to refine or overwrite the class. As shown in 8.3. The super-Class shape in the figure specifies two interface methods for implementing polymorphism, Computearea () and Computevolume (). Subclasses, such as circle and sphere, refine or overwrite both interface methods in order to achieve polymorphism.
4.flexibility (flexibility). It embodies the flexible operation in the application, and improves the use efficiency.
5.simplification (simplicity). Polymorphism simplifies the process of coding and modifying the application software, especially when dealing with the operation and operation of a large number of objects, which is particularly prominent and important ..
This article is from the "8935721" blog, please be sure to keep this source http://8945721.blog.51cto.com/8935721/1681055
Polymorphism in Java