"Perverted" class.

Source: Internet
Author: User

Take a look at the following "perverted" classes

Parentchildtest.java Example

public class Parentchildtest {

public static void Main (string[] args) {

Parent parent=new parent ();

Parent.printvalue ();

Child child=new Child ();

Child.printvalue ();

Parent=child;

Parent.printvalue ();

parent.myvalue++;

Parent.printvalue ();

((child) parent). myvalue++;//

Parent.printvalue ();

}

}

Class parent{

public int myvalue=100;

public void Printvalue () {

System.out.println ("Parent.printvalue (), myvalue=" +myvalue);

}

}

Class Child extends parent{

public int myvalue=200;

public void Printvalue () {

System.out.println ("Child.printvalue (), myvalue=" +myvalue);

}

}

1. What is the result of the program operation? 2. How do you explain that you will get such output? 3. The computer is not wrong, the reason why you get such a running result is also a reason, then from these running results, you can summarize the Java syntax features?

Program Run Result:

Explain:

The parent class object is defined as a subclass type (a family materialization)

Now that the subclass inherits the parent class, the subclass is more representative of the specific behavior of the family, and the parent class has properties and methods of the parent class when the object is defined as a subclass type.

In turn, child class objects cannot be defined as parent class types

Why can't a subclass object be defined as a parent class type? Because the subclass inherits from the parent class, therefore, the subclass has more methods than the parent class, because in addition to inheriting from all the properties and methods of the parent class, the subclass may have defined other methods, so in general, the magnitude of the subclass is larger than the parent class, so the parent class object can be defined as a subclass object, because the extra method can directly use the subclass's , and the converse is not, because the subclass object does not know how to cut out the extra methods.

So, in a word, a small box (the parent object) can be put into a large box (subclass), the large box (subclass object) cannot be nested in a small box (parent class).

Similarly, a parent object can be assigned a value (defined as a subclass type) on a class object, and the subclass object cannot be assigned a value by the parent object.

A parent class object can directly control properties in an object only if it is explicitly converted to a subclass object, but the method of assigning a parent class object to a subclass type does not allow the parent class object to fully control the properties in the object.

If the child class has the same field as the parent class , The fields in the subclass are substituted or hidden from the fields of the parent class, and the fields in the subclass are accessed (not the fields in the parent class).

In other words, once the subclass parent has a field of the same name, it complicates the inheritance, and the parent class of the child class has a variable or method that is fading and is not easy to invoke.

For example, the parent subclass is known as f The property or method, then, in the case of inheritance, Subclasses will have two properties or methods, One inherits from the parent class (defined by the parent class), and the other is defined by the subclass itself. When a subclass object is generated, the subclass object is called once f property or execution f f method, if you want to forcibly use the parent class's f property or Execute parent class f method, you have to use super. F () to invoke.

Summary: (1) When a subclass has the same method as the parent class, and a parent class variable refers to a subclass object, which method is called, determined by the object's own "true" type, that is, the object is a subtype, it calls the method of the subtype, it is the parent type, and it invokes the method of the parent type.
This feature is actually a concrete representation of the object-oriented "polymorphic" feature.

(2) If the subclass has the same field as the parent class, the fields in the subclass are substituted or hidden from the fields of the parent class, and the sub-class method accesses the fields in the subclass (not the fields in the parent class). If the subclass method does want to access a field that is hidden in the parent class with the same name, it can be accessed with the Super keyword.
If the subclass is used as a parent class, the field accessed through the subclass is the parent class!
Keep in mind: (Verification conclusion Post blog)
In real-world development, avoid defining a field with the same name as the parent class in the child class. Don't ask for trouble! --but except for exams, it is still possible to have this problem in the exam.

"Perverted" class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.