Scjp test preparation-6

Source: Internet
Author: User

 

Question 1: The execution result of the following code:

class Person{     String name = "No name";     public Person(String nm){name = nm;} } class Employee extends Person{     String empID = "0000";     public Employee(String id){empID = id;} } class EmployeeTest{     public static void main(String[] args){         Employee e = new Employee("4321");         System.out.println(e.empID);     } }

Question 2: The execution result of the following code:

class Atom{     Atom(){System.out.print("atom ");} } class Rock extends Atom{     Rock(String type){System.out.print(type);} } public class Mountain extends Rock{     Mountain(){         super("granite ");         new Rock("granite ");     }     public static void main(String[] a){new Mountain();} }

Question 3: which adjustments can be made to the following code to enable normal sub class execution:

1.  class Super{ 2.   private int a; 3.   protected Super(int a){this.a = a;} 4.  } ...  11.  class Sub extends Super{ 12.   public Sub(int a){super(a);} 13.   public Sub(){this.a = 5;} 14.  }

Adjustment options:

A.  Change line 2 to:      public int a; B.  Change line 2 to:      protected int a; C.  Change line 13 to:      public Sub(){this(5);} D.  Change line 13 to:      public Sub(){super(5);} E.  Change line 13 to:      public Sub(){super(a);}

 

The focus of these two questions is class inheritance. The test site is the call of the parent class constructor.

Note the following when calling the parent class constructor:

  1. Subclass does not obtain the constructor of the parent class;
  2. You can use this to call another overloaded constructor in one constructor. You can use super to call the parent class constructor In the subclass constructor;
  3. Using Super to call the parent class constructor must appear in the first row of the sub-class constructor execution body;
  4. No matter whether super is used to call the parent class constructor, The subclass constructor always calls the parent class constructor. There are three calling methods:
    • The subclass constructor uses super to explicitly call the parent class constructor;
    • The subclass constructor uses this to call the overloaded constructor. The overloaded constructor calls the parent constructor;
    • If the sub-class constructor does not have a super call or this call, the system will implicitly call the non-parameter constructor of the parent class before executing the sub-class constructor.
  5. When the subclass constructor is called to initialize the subclass object, the parent class constructor always runs before the subclass constructor. Create any Java object and execute the object class constructor first;
  6. If a custom constructor is not provided for a class, the system provides a non-parameter constructor for the class by default. Otherwise, if a custom constructor is provided for the class, the system will not provide an implicit parameter-free constructor for this class.

 

Question Analysis: in question 1, the person class does not have a parameter constructor, And the constructor of the sub-class employee does not use super to call the defined constructor in the person class, then the child class cannot find the person class constructor without parameters and cannot be compiled.

Attached Question 1 Compilation result:

 

The execution result of Question 2 can be inferred based on the fifth article above.

Result 2:

 

For question 3, first analyze the given code and focus on the second constructor of the sub class. This constructor needs to call the non-argument constructor of the parent class implicitly. The parent class does not provide a parameter-free constructor. Therefore, you need to add a non-argument constructor for the parent class super, or adjust the second constructor of the sub class to call the parameter constructor of the super class. In the answer, the CD is correct, and the scope of the parent class member variable a called in E is incorrect.

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.