Polymorphism (polymorphism) is a property of object-oriented programming, which allows multiple ways to use the same interfaces. Java supports polymorphism in many ways, both of which the most prominent. The first is that each method (except the methods marked as final) can be rewritten quilt class; the second is the establishment of interface keyword. The following will give a detailed description of these two areas.
As the super-class method can be overridden in a derived class, so create a class hierarchy is very simple. In the class hierarchy, each sub-class of its super class specialization (specialization). As we all know, a super-class reference can refer to any one of its subclasses, but also through the superclass passed by reference a sub class objects in a way, it will automatically implement Yougai rewritten the version of the subclass.
Therefore, we can use the super class to define the form of an object and provides default implementation of the object, while the sub-categories according to which the default implementation be modified to better meet the specific requirements. Therefore, in the super class defines an interface can be used as the basis for many different implementations.Of course, Java and further to the "one interface, multiple methods" concept. It defines the interface keyword, so you can be class methods and class to achieve complete separation. Although the interface is abstract, but can still be declared interface type reference. This concept is important because it can improve the application of polymorphism. Whenever a class implements an interface, and the interface provides a feature, then any need for such functionality of the code can use this class Duixiang. For example, suppose an interface name for the MyIF, consider the following method:
void myMeth (MyIF ob) (
/ / ...
)
Any realized MyIF interface object can be passed to myMeth () method. Other features of the object without the need to consider. myMeth () method can be implemented on any object MyIF interface to operate.
No comments:
Post a Comment