Does the Java face question 60 interface inherit the interface? is an abstract class achievable (implements) interface? Can abstract classes inherit concrete classes (concrete class)? Can I have a static main method in an abstract class?

Source: Internet
Author: User

Interfaces can inherit Interfaces. Abstract classes can implement (implements) interfaces, and abstract classes can inherit concrete Classes. There can be static main methods in an abstract class.

Q: Does an abstract class inherit entity classes (concrete Class)

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

The answer is clear and can be Inherited. In fact, from object is an entity class, Java API document, each abstract class entry is explicitly written directly or indirectly inherit from object, so this is no doubt.

The key is that the answer to the "premise is that the entity class must have a clear constructor", what is the meaning of the sentence.

General learners will write a simple test code:


Class a{}

Abstract class B extends a{}

The result is completely normal, compiled Through. It does not seem to have anything to do with the "entity class must have a definite constructor".

This issue involves two basic knowledge:

1. All classes must have a constructor method, and if you do not declare the constructor in the code, the system will automatically generate a public and non-parametric construction Method. And as long as you declare a method of construction, regardless of the argument, the private public, the system will not help you to generate a default parameterless Constructor.

2. All subclass constructors require that the parent class constructor be called 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 defaults to the method is also counted, class a{} code is actually

Class a{

Public A () {}

}

b When you inherit a, It's

Abstract class B extends a{

Public B () {

Super ();

}

}

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

Class a{

Private A () {}


}

This time, as the basic knowledge (1) said, The system no longer gives you the default parameterless constructor, B's constructor According to the rules in (2) to call Super (), but can not find a of the parameterless constructor, so that the abstract class B extends a{} compilation cannot pass. (because there is no constructor in a that can be called by a subclass, this time a can only be inherited by an internal class, and the 3.4 version of Eclipse I used 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, it 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 subclass can access the parameterless constructor, also the same, the subclass can write nothing, with the default mechanism Called.

3. If you write an argument constructor without a parameterless constructor, the parent class has no access to the parameterless constructor, and the subclass must specify in the first sentence of the subclass constructor that it calls the parent class with the parameter constructor and passes the parameters in.

4. Classes that are declared final and that all constructors are not within the subclass access permission cannot inherit

In fact, as long as the inheritance of the class, whether abstract or entity, it is necessary to conform to this rule. At any time in this succession experiment, the result of deleting or adding an abstract prefix has not changed. Personally feel that "entity class must have a clear constructor" sentence is not able to express the situation clearly, so the majority of job seekers still write clearly better.

The way I like to do this is " can inherit, but as with the inheritance of the entity class, the parent class is required to inherit and have the constructor that the subclass can access ." ”

There can be static main methods in an abstract class.

 public Abstract class Test14 {publicstaticvoid  main (string[] Args) {System.out.println ("abc ");}}

Running Result: ABC

This program can run, although you are defining the class Test14 is Abstract. But there is no abstract method in your class, and the actual class is non-abstract, so you can run it if you define an abstract method Fangfa () and use this abstract method FANGFA () in the main method.

Abstract modifier

Abstract Class:

An abstract class cannot be used to instantiate an object, and the sole purpose of declaring an abstract class is to augment the class in the Future.

A class cannot be both abstract and final Decorated. If a class contains an abstract method, the class must be declared as an abstract class, or a compilation error will Occur.

Abstract classes can contain both abstract and non-abstract METHODS.

Abstract methods

An abstract method is a method that does not have any implementation, and the specific implementation of the method is provided by the SUBCLASS.

Abstract methods cannot be declared final and Static.

Any child class that inherits an abstract class must implement all the abstract methods of the parent class, unless the subclass is also an abstract class.

If a class contains several abstract methods, the class must be declared as an abstract class. Abstract classes can contain no abstract methods.

The declaration of an abstract method ends with a semicolon, for example: publicabstract sample ();.

Does the Java face question 60 interface inherit the interface? is an abstract class achievable (implements) interface? Can abstract classes inherit concrete classes (concrete class)? Can I have a static main method in an abstract class?

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.