Can an abstract class inherit an entity class?

Source: Internet
Author: User

A common Java interview problem, found on the internet almost every Java interview pen question book or collection can find this problem.

The topics are as follows:

Question: Whether an abstract class can inherit an entity class (concrete Class)

A: An abstract class can inherit an entity class, but only if the entity class must have a definite constructor

The answer is clear and can be inherited. In fact, from object is an entity class, Java API documents, each abstract class entries are explicitly written directly or indirectly inherited from object, so this is not a question.

The key is what the answer says, "The premise is that the entity class must have a definite constructor".

A simple test code that a typical learner would write:

Class a{}

Abstract class B extends a{}

The result is completely normal and the compiler passes. It seems to have nothing to do with "entity classes must have a definite constructor".

This issue involves two basic knowledge:

1.

All classes must have a construction method, and if you do not declare the construction method in your code, the system will automatically generate a public parameterless construction method. And as long as you declare a construction method, regardless of the parameter, private public, the system will no longer help you generate the default parameterless constructor.

2.

All subclass constructors require the parent class constructor to be invoked in the first line of code, and if not written, the system defaults to calling the parent class's parameterless constructor.

So, if the system default rationing method is also included, the class a{} code is actually

Class a{

Public A () {}

}

b when it inherits a, it is

Abstract class B extends a{

Public B () {

Super ();

}

}

To experiment with the internal situation of this inheritance rule, it is also very simple, in the top of the simple test code, plus a private constructor, there are no parameters.

Class a{

Private A () {}

}

This time, as the basic knowledge (1) says, the system no longer gives you the default parameterless constructor, B's constructor calls super () according to the Rules in (2), but cannot find a parameterless constructor, so the compilation of abstract class B extends a{} cannot pass. (Because there is no constructor in a for subclasses to invoke, this time a can only be inherited by the inner class, and my 3.4 version of Eclipse would recommend renaming B, but that doesn't solve the problem.) )

Now, you should know the meaning of the vague "entity class must have a definite constructor" for the data:

1. The constructor is not written, that is the default parameterless public constructor, the subclass can write nothing, let the default constructor to call it. This is the case for the first two lines of code.

2. The parameterless constructor that the subclass can access is also the same as the subclass can write nothing and invoke the default mechanism.

3. If a parameter constructor is written without a reference constructor, there is no parameterless constructor accessible by the subclass in the parent class, and the subclass must specify the first sentence in the subclass constructor, invoke the parent class with the parameter constructor, and pass the arguments in.

4. A class that is declared final and all constructors are not within the subclass access permission cannot be inherited

In fact, as long as in the inheritance of the class, whether abstract or entity, you need to conform to this rule. At any time in this inheritance experiment the prefix of the deletion or the addition of abstract is not changed. The individual feels that "entity classes must have a definite constructor" is really not able to express this situation clearly, so the broad masses of job seekers still write clearly better.

What I like to do is "you can inherit, but like the entity class inheritance, you also require that the parent class be inheritable and that you have a constructor that the subclass can access." ”

I only answer "can inherit" in the written test. Thinking that all classes inherit from the object class, and that the second half of the sentence is lost.


Http://hi.baidu.com/maso88/item/a96f9a8e7fa2e7d15e0ec1d2

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.