JAVA polymorphism only for methods not for attributes

Source: Internet
Author: User

1 polymorphic is only for methods, not attributes, but on the notation, subclasses override the parent class's properties, the compiler will not error

Class person{
String name = "Person";

public void shout () {
SYSTEM.OUT.PRINTLN (name);
}
}

Class Stu extends person{
String name = "Student";
String School = "school";
}
public static void Main (string[] args) {
Person p = new Stu ();
P.shout ();
}
The answer is person.

If you change the person p = new Stu () in the main method to Stu s = new Stu ()
S.shout () Enter what.
The output is also person, because the subclass Stu does not override the parent class method shout (), so when calling the Shout method,
The properties referenced in the Shout method will be super. Properties. As far as property inheritance is concerned, it is indeed overridden, but the method in the parent class
The properties of the child class are not referenced in the. (You can output a method of a parent class in a subclass, because the child class has no way of overriding the parent class)


The teacher says the subclass inherits the parent class, and there is a copy of all the properties and methods of the parent class that are not private.
Then my p.shout () is the method that invokes the copy of itself. Instead of the parent class, the shout () method is this? I ran the problem. I found the teacher's words in my hand. Thank you for your answer.

First, whether you are person P = new Stu (); or Stu Stu = new Stu (),
The results are all output
Property values of the parent class

2 is also about the properties polymorphism

Class person{
String name = "Person";
}

Class Son extends Person {
String name = "Son";
}
public class Test {

Person p = new Son ();
System.out.println (P.name);

}
The result is person.
Why not son?
A variable is a static binding, by dynamic binding. Static binding is the type of variable itself, which is used in which. For example, if your p.name p is the person type, then name is the name in the person class. In the case of dynamic binding, the type is searched for the superclass starting from its own. If name is a method, then for P is an object of the son class, so the name method is searched from son, if it is not found in the parent class.

Because person p = new Son ();

is to transform the handle class into a parent class

The subclass transforms to the rule of the parent class:

    1. Only properties of the parent class can be called

    2. Only methods that are not replicated by the parent class can be called

    3. You can only call the quilt-type replication party

JAVA polymorphism only for methods not for attributes

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.