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:
Only properties of the parent class can be called
Only methods that are not replicated by the parent class can be called
You can only call the quilt-type replication party
JAVA polymorphism only for methods not for attributes