Sometimes the parent class object needs to be pointed to the subclass object, and sometimes the parent class object needs to be converted to the subclass object. For example, we can say that a dog is an animal and a dog, but the premise is that we must know that a dog must be an animal, the animal must be a dog. The following describes how to direct the parent class to a subclass object, and how to forcibly convert a subclass to a subclass object that is preferred by the subclass.
1. parent class references to subclass objects
Under normal circumstances, we will define a Class Object Reference, then use new to instantiate an object, and then point this reference to the instance. For example:
Dog dog = new dog ();
If dog is a subclass of animal, you can also write the following code:
Animal animal = new dog ();
Animal animal defines an animal object reference. New dog () instantiates a dog and animal points to the dog. This is allowed, just as we point to a dog and say it is an animal. It is indeed an animal.
You can also write the following form:
Animal animal = dog;
This means that the animal reference points to the instance referenced by dog.
This is the upward transformation, not only the parent class can point to the subclass object, as long as the ancestor class can point to the subclass instance. In typical cases, an object is the ancestor class of all classes, so object reference can point to any object. For example, the following code:
Object o = new dog ();
What are the advantages of pointing a parent class to a subclass object? You can think about it as follows.
In addition, similar usage is as follows:
A method is defined as follows:
Public void setvalue (Object O ){
This. value = O;
}
The parameters of this method are of the object type, and the return value type is of the object type. Because the parameter type is object, when calling this method, we can pass any type of parameter to it, including the above dog object, as long as it is its Child class.
Let's look at the following method:
Public object getvalue (){
Return value;
}
Value can be any type, dog instance, animal instance, date instance, or person instance.
Think about the linked list and queue in the data structure. Their elements can be of various types. In this way, you can set an integer or dog linked list to obtain its elements, the type of the returned value is determined by the element type, so both the parameter and return value types should be set to object.
To sum up one sentence: any place where the parent class object is required can be assigned to the subclass object.
2. Forced type conversion
We can point the parent class reference to a subclass object, but this brings other problems. Assume that dog has a special behavior method F. If the dog instance is assigned a reference to animal, for example, the following code:
Animal animal = new dog ();
How to access the F method of dog?
Writing animal. F () directly is definitely not acceptable. Compilation fails because animal has no method F ().
Therefore, you need to switch animal to dog to access method F. Can it be written like this?
DOG d = animal;
No. An error Type Mismatch will be reported during variation. You can use the following code:
DOG d = (DOG) animal;
Forcibly converts animal to dog, that is, converts an animal to a dog. This is forced type conversion, which converts the parent class object to a subclass object (The subclass object references the parent class object ).
Can it be converted? Because we know that this animal is indeed a dog, it can be switched.
Let's look at the following code:
Animal animal = new CAT ();
DOG d = (DOG) animal;
There is no difference from the previous Code in line 1. But you can see at a glance that there is a problem, because the code wants to convert the cat into a dog, this will report an error: classcastexception
This error is not a syntax error, so it can be compiled and passed, but it fails during running.
Therefore, when performing a forced type conversion, we must ensure that the conversion can be performed only when the instance to which the parent class points is an object of a subclass.
If you are not sure, you can use the instanceof operator to determine.
If (animal instanceof dog)
DOG d = (DOG) animal;
Last time: 39th speaking of Polymorphism
Next time: 41st talk about this and super
Li xucheng csdn blog: Why? U= 124362 & C = 7be8ba2b6f3b6cc5