Reference Link: http://www.cnblogs.com/jack204/archive/2012/10/29/2745150.html
The main concept: inheritance, rewriting, to increase flexibility,
-
polymorphic definition : means that objects of different classes are allowed to respond to the same message. That is, the same message can be used in a variety of different ways depending on the sending object. (Sending a message is a function call)
-
Three prerequisites for polymorphic existence: To have an inheritance, to have overrides, a parent class reference to a subclass object . The 3rd meaning is:
For example, parent classAnimal, sub-classCat,dog. WhichAnimalcan be either a class or an interface,Catand theDogis inherited or implementedAnimalsub-class.
Animal Animal = new Cat ();
That is, the parent class is declared, which actually points to an object of the subclass. What does that mean?
So what are the advantages of using this? You can use these keywords to summarize: polymorphic, dynamic link, upward transformation.
very simple, it means I have defined a reference to a animal type, pointing to the new cat type object. because Cat inherits from its parent class animal, references to animal types are objects that can point to cat types. This is the "upward transformation".
> So what's the point of doing that? because subclasses are an improvement and an extension of the parent class, the generic subclass is more powerful than the parent class, the property is more unique than the parent class, and the object that defines a reference to a child class for a parent class can use both the powerful functions of the subclass and the commonality of the parent class. Therefore, a reference to a parent class type can call all properties and methods defined in the parent class, whereas a method that is not in the parent class defined in the subclass is a < span>
What is a dynamic link? When a method in a parent class is defined in a parent class and not overridden in a subclass, it can be called by a reference to the parent class type, and for a method defined in the parent class, if the method is overridden in a subclass, a reference to the parent class type will invoke this method in the subclass, which is the dynamic join.
See an example:
Class father{
public void Func1 () {
Func2 ();
}
This is the Func2 () method in the parent class, because the method is overridden in the following subclass
So when called in a reference to a parent class type, this method is no longer valid
Instead, the Func2 () method that will be overridden in the calling subclass
public void Func2 () {
System.out.println ("AAA");
}
}
Class Child extends father{
func1 (int i) is an overload of the Func1 () method, not primarily a rewrite!
Because this method is not defined in the parent class, it cannot be called by a reference of the parent class type
So in the following Main method, CHILD.FUNC1 (68) is wrong.
public void func1 (int i) {
System.out.println ("BBB");
}
Func2 () overrides the Func2 () method in the parent class father
If the Func2 () method is called in a reference to the parent class type, then this method must be overridden in the subclass
public void Func2 () {
System.out.println ("CCC");
}
}
public class Polymorphismtest {
public static void Main (string[] args) {
Father child = new Child ();
CHILD.FUNC1 ();//What will the print result be?
CHILD.FUNC1 (68);
}
}
The above program is a very typical polymorphic example. Subclass Child inherits the parent class father and overloads the Func1 () method of the parent class, overriding the Func2 () method of the parent class. The overloaded func1 (int i) and func1 () are no longer the same method, and because there is no func1 (int i) in the parent class, the reference child of the parent class type cannot call the func1 (int i) method. When the subclass overrides the Func2 () method, the reference to the parent class type will call the FUNC2 () overridden in the subclass when calling the method.
The result of this printing is: "CCC"
Reference: http://www.cnblogs.com/ChrisMurphy/p/5054256.html
Three ways to implement polymorphic implementations in 3.Java: interface implementations, inheriting parent classes for method overrides, and method overloading in the same class.
4. method Overrides, overloads, and dynamic joins form polymorphism. One of the reasons why Java introduces the concept of polymorphism is that it differs from C + + on the inheritance of classes, which allows multiple inheritance, which does give it very powerful functionality, but complex inheritance relationships also bring more trouble to C + + developers, in order to avoid risk, Java only allows single inheritance, and there is a is-a relationship between the derived class and the base class ("Cat" is a "animal"). Although this ensures that the inheritance relationship is simple and clear, but there is bound to be a large number of functional limitations, so Java introduced the concept of polymorphism to compensate for this shortcoming, in addition, abstract classes and interfaces are also important means to solve the single inheritance restrictions. At the same time, polymorphism is also the essence of object-oriented programming.
This article is from the "7124048" blog, please be sure to keep this source http://7134048.blog.51cto.com/7124048/1769425
Java understanding of Polymorphism