1. What is the difference between super and this?
Super is a keyword that we can use when we want to use a member variable or method of a parent class that is hidden by the quilt class in a subclass.
This appears in an instance method of the class, representing the current object that uses the method. It can also appear in the constructor of a class, representing an object created using the constructor method.
This () represents the current class object, and Super () represents the parent class object.
Represents access to the members and behaviors of the parent class in the subclass, which must be constrained by the class inheritance rules.
In the constructor of a subclass, if the first row does not write super (), the compiler inserts it automatically. However, if the parent does not have a constructor with no arguments, or if the constructor is privatized, you must include the instantiation construct of the parent class. This does not have this requirement, because it is an instantiation of the structure itself.
The use of super and this in the method is pretty much the same, except that super has to consider accessing the resources of its parent class.
2. Order of execution of inherited time classes:
Please see source code:
A. Parent class
Package test;
public class fatherclass{
Public Fatherclass () {
System.out.println ("Fatherclass Create");
}
}
B. Sub-class
Package test;
Import Test. Fatherclass;
public class ChildClass extends fatherclass{
Public ChildClass () {
System.out.println ("ChildClass Create");
}
public static void Main (string[] args) {
Fatherclass FC = new Fatherclass ();
ChildClass cc = new ChildClass ();
}
}
Please write out the output of the B neutron class after execution.
Program execution first looked for the main method, and the result of the first sentence execution is:fatherclass Create.
since the first line in the subclass's construction method hides super (), when the second sentence is executed, the constructor of the parent class is called first, so the output:fatherclass Create, and finally calls the subclass's own constructor, output: ChildClass Create
So the answer is:
Fatherclass Create
Fatherclass Create
ChildClass Create
This article is from the Java interview blog, so be sure to keep this source http://funyou.blog.51cto.com/8580577/1539160