Friday, September 3, 2010

Polymorphism of Java

 As we all know, JAVA has three characteristics, namely encapsulation, inheritance, polymorphism. Encapsulation and inheritance is not to say, is to package the data and method of operation of data tied to prevent misuse of the outside. Inheritance is a class of complex code in another class. Focus for that polymorphism. Polymorphism, by definition, a variety of forms. In JAVA where is the "one interface, a number of ways." Okay, the 'many ways' is relative ...

As we all know, JAVA has three characteristics, namely encapsulation, inheritance, polymorphism. Encapsulation and inheritance is not to say, is to package the data and method of operation of data tied to prevent misuse of the outside.


Inheritance is a class of complex code in another class. Focus for that polymorphism. Polymorphism, by definition, a variety of forms. In JAVA where is the "one interface, a number of ways." Okay, the 'many ways' is relative to the interface implementation class for the. An interface defines abstract methods of N, the interface implementation class M a N a way to achieve this through the N-way set up a reference to contact his son - "reference point to subclass the parent class object."

That is, I can use the interface reference to any call type for each interface that N is a solution. Once again, Okay, this N-way interfaces to the implementation class, defined by their method of implementation class is not this column! That go through a lot of whole body's role is to: strengthen the code scalability and maintainability.

Procedure to add new features, as long as the corresponding increase in an implementation class on OK, so the code is very good extension. BUG a program or a new wine bottle to install, easy to handle, find the appropriate implementation class change it Over, other places do not control. Two polymorphisms of expression: Overload (overload) and Override (rewrite). Overloading is a type of polymorphism in a reflection.

Override the parent class and subclass polymorphism between a reflection.
To talk about the rewrite. If the child class defines a method with the parent class has the same name and parameters, we say that the method has been rewritten. Subclass object using this method is called sub-class definition, it is concerned, the definition of the parent class as being "shielded" the. If you still want to use the parent class method, prior to plus super keyword. Of course, the same applies here, "pointing to subclass the parent class of reference objects." Let me say something heavy.

If a class defines several methods of the same name, but the method of the parameters are different, then known as the method of overload. Overloaded methods can change the return value type.
Here Okay, overloaded methods can thoroughly are two different methods, and equivalent to subclass the parent class does not define its own way. Therefore, the "reference point to subclass the parent class object" does not apply here.

No comments:

Post a Comment