How is Java polymorphism implemented?
Java polymorphism, like C + +, is accomplished by delay binding (late binding) or Run-time binding (runtime Binding). When a method referenced by an object is invoked, the compiler does not know whether the reference refers to the type object that is described in the variable declaration, or the object of the subclass of that type. Therefore, the compiler cannot bind to a specific method for this call. Only the Run-time type recognition (RTTI, Runtime type identification) in Java is bound to a specific method at run time. The following is a specific example:
Copy Code code as follows:
Class shape
{
public void Draw ()
{
Print ("shape");
}
}
Class Triangle extends shape
{
public void Draw ()
{
Print ("triangle");
}
}
public class Polymorphism {
public static void Main (string[] args)
{
Shape s=new triangle ();
S.draw ();
}
The result is triangle.
S is a shape reference, but it calls the Triangle Draw method at run time because it is a triangle object.
Some pitfalls in Java polymorphism
Override Private Method?
In Java, a private method cannot be overridden, which is well understood, because private methods are not visible in subclasses. The subclass does not inherit the private method of the parent class, not to mention rewriting it. Therefore, the method of the same name in a subclass is a completely new method.
Copy Code code as follows:
public class Polymorphism {
private void Show ()
{
Print ("show parent");
}
public static void Main (string[] args)
{
Polymorphism P=new Privatemethod ();
P.show ();
}
}
Class Privatemethod extends polymorphism
{
public void Show ()
{
Print ("show derived");
}
}
The result is show parent
Polymorphism of fields and static methods?
Subclasses can inherit non-private fields from the parent class, and do the fields of subclasses also have polymorphism? Let's look at a practical example:
Copy Code code as follows:
Class shape
{
protected int perimeter=1;
public void Draw ()
{
Print ("shape");
}
public int Getperimeter ()
{
return perimeter;
}
}
Class Triangle extends shape
{
int perimeter=3;
public void Draw ()
{
Print ("triangle");
}
public int Getperimeter ()
{
return perimeter;
}
public int Getsuperperimeter ()
{
return super.perimeter;
}
}
public class Polymorphism {
public static void Main (string[] args)
{
Shape s=new triangle ();
Print ("S.perimeter:" +s.perimeter);
Print ("S.getperimeter:" +s.getperimeter ());
Triangle t=new triangle ();
Print ("T.perimeter:" +t.perimeter);
Print ("T.getperimeter:" +t.getperimeter ());
Print ("T.getsuperperimeter:" +t.getsuperperimeter ());
}
}
The following are the results of the operation:
This run result contains the following information:
When a 1.triangle object is converted up to shape, direct access to the field is determined by the compiler, so it does not show polymorphism and returns 1.
When the 2.triangle object transitions up to shape, the calling method access field calls the Triangle Getperimeter method based on the Run-time object type delay binding, which returns 3
The 3.t object contains two perimeter fields, one from himself and one from his parent class. When you call the field with the field name, the perimeter field is returned by default, and the field that inherits from the parent class is called by using the Super.perimeter method.
The result seems somewhat confusing, and in order to avoid this, we generally declare the field as private (subclasses cannot inherit), and the fields we declare in subclasses are best not to have the same name as the fields that inherit from the parent class.
Static methods are not polymorphic because static methods are bound to the class and do not exist without knowing the specific type.
The polymorphism of a constructor?
Constructors are not polymorphic because the construction method itself is a static method (if not, it will be caught in a chicken egg, a dead cycle of egg-laying chickens). To introduce our problems, first look at the order in which constructors are called.
1. The storage space allocated for this object is initialized to 0 (object initialized to null)
2. Constructor invocation of the parent class (this ensures that the field accessed in the constructor of the subclass is initialized)
3. Member Variable initialization
4. Constructor call to subclass
Now suppose that if in the second step we call a method in the constructor of the parent class, is this method polymorphic? Let's look at a specific example:
Copy Code code as follows:
Class shape
{
protected int perimeter=1;
Public shape ()
{
Draw ();
Print ("shape created");
}
public void Draw ()
{
Print ("Draw shape" +perimeter);
}
}
Class Triangle extends shape
{
int perimeter=3;
Public triangle ()
{
Print ("triangle created");
}
public void Draw ()
{
Print ("Draw triangle" +perimeter);
}
public int Getperimeter ()
{
return perimeter;
}
}
public class Polymorphism {
public static void Main (string[] args)
{
Shape s=new triangle ();
}
}
Run Result:
We can see that although the Triangle object has not yet been constructed, the draw method is still dynamically bound to the triangle draw method. Also note that the value of perimeter is not initialized to 3, but 0.
The result is that we have accessed the fields before the Triangle object has been initialized. So we want to avoid calling other methods in the constructor, or just calling the private method (not inherited and therefore not raising the problem)