Java interview often test the east

Source: Internet
Author: User

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

Related Article

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.