Whether the abstract class can inherit the object class

Source: Internet
Author: User
  1. Analysis on whether an abstract class can inherit an object class.
  2. A common Java interview question can be found on the Internet in almost every Java interview question or collection.
  3. The questions are as follows:
  4. Q: Can an abstract class inherit a concrete class)
  5. A: abstract classes can inherit object classes, provided that object classes must have clear constructors.
  6. The answer is clear and can be inherited. In fact, an object is an entity class. In the Java API documentation, the entries in each abstract class clearly indicate that they are directly or indirectly inherited from the object, so there is no doubt about this.
  7. The key lies in what it means by saying "the premise is that the entity class must have a definite constructor.
  8. Simple trial code written by General learners:
  9. Class {}
  10. Abstract class B extends {}
  11. The result is completely normal and the compilation is successful. It seems that it has nothing to do with "the entity class must have a clear Constructor.
  12. This problem involves two basic knowledge:
  13. 1.
  14. All classes must have a constructor. If you do not declare the constructor in the Code, the system will automatically generate a constructor with no public parameters. As long as you declare a constructor, the system will no longer generate the default constructor for you, regardless of whether the parameter is set or public.
  15. 2.
  16. All subclass constructors require that the parent class constructor be called in the first line of code. If no data is written, the system calls the non-argument constructor of the parent class by default.
  17. Therefore, if you calculate the default Assigned method, the code of Class A {} is actually
  18. Class {
  19. Public (){}
  20. }
  21. When B inherits a, it is
  22. Abstract class B extends {
  23. Public B (){
  24. Super ();
  25. }
  26. }
  27. To test the internal situation of this inheritance rule, it is also very simple. In the simple test code at the top, add a private constructor, and there are no parameters.
  28. Class {
  29. Private (){}
  30. }
  31. At this time, as stated in basic knowledge (1), the system no longer gives you the default no-argument constructor. The constructor of B calls super () according to the rules in (2 (), however, the non-argument constructor of A is not found, so the compilation of abstract class B extends a {} fails. (Because there is no constructor in a that can be called by sub-classes, in fact, a can only be inherited by internal classes at this time. I recommend that you change the name of B in eclipse version 3.4, but this cannot solve this problem .)
  32. Now, you should understand the vague meaning of the document "the entity class must have a clear constructor:
  33. 1. If the constructor is not written, it has a default public constructor without parameters. The sub-class can do nothing and let the default constructor call it. This is the first two lines of code.
  34. 2. Write the non-argument constructor that can be accessed by sub-classes. The sub-classes can be called by default instead of writing anything.
  35. 3. if the parameter constructor is written but the parameter constructor is not written, there is no sub-class accessless parameter constructor in the parent class. The sub-class must be written in the first sentence of the sub-class constructor, call the parameter constructor of the parent class and pass the parameter in.
  36. 4. Classes declared as final and all constructors that are not within the subclass access permission cannot be inherited.
  37. In fact, as long as the class is inherited, no matter the abstraction or entity, it must comply with this rule. In this inheritance experiment, the results remain unchanged if the prefix of abstract is deleted or added at any time. I personally think that the phrase "entity classes must have clear constructor" cannot clearly express this situation, so the majority of job seekers can still write clearly.
  38. My favorite syntax is "yes, but like the inheritance of object classes, it also requires that the parent class can be inherited and has a constructor that can be accessed by sub-classes ."
  39. ---------------------
  40. If any of your friends finds any omissions in this article, I hope you will not give me any further advice. Thank you.
  41. In addition, if you need to reprint it, please indicate the source.

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.