15, the interface is inheritable interfaces? Are abstract class can implement (implements) the interface? Abstract class is inheritable specific category (concreteclass)? Can I have a static main method of abstract class?

Interface can be inherited interfaces. Abstract class can implement (implements) interface abstract class can inherit specific class. You can have static main method of abstract class.

 

Q: Abstract class is inheritable entity class (concrete class)

A: An abstract class can be inherited entity classes, but only if the entity class must have explicit constructor.

The answer is clear, it can be inherited. In fact, from that entity Object class, the Java API documentation, each entry in the abstract class are clearly written, directly or indirectly inherited from Object, so this is no doubt.

The key lies in the answer said, "provided that the entity class must have explicit constructor" one, what that means.

 

Simple test code is generally learners will write:


class A{}

abstract class B extends A{} 

The results completely normal, compiled by. It seems "entity class must have explicit constructor" nothing to do.

This issue involves a two basics:

1. All of the class must have a constructor, if you do not declare the constructor in the code, the system will automatically give you generate a public constructor with no arguments. And as long as you declare a constructor, with or without ginseng ginseng, private public, the system does not help you generate a default no-argument constructor.

2. All subclasses require constructor call the parent class constructor in the first line of code, if not written, the system defaults to call the parent class's constructor with no arguments.

 

Therefore, if the method is also assigned to the default enumerated, class A {} code is actually

class A{

  public A(){}

 

B A succession of time, it is

abstract class B extends A{

  public B(){

    super();

  }

 

To test the internal rules of succession in this case, is also very simple, at the top of the simple test code, plus there Private constructor, there is no reference parameters do.

class A{

  private A(){}


}

This time, such as (1) mentioned in the basics, the system does not give you a no-argument constructor default constructor B in accordance with (2) the rules to call super (), but can not find no arguments constructor A device, resulting in abstract class B extends A {} does not compile. (Because there are no A constructor for a subclass called, in fact, this time the A class inherits only be used internally, I use version 3.4 of the Eclipse will suggest to B renamed, but it can not solve this problem.)

 

Now, you should understand the information to the vague phrase of "entity class must have explicit constructor" means:

1. I did not write a constructor, that is to have the default no-argument public constructor subclass can write nothing, so the default constructor to call it. This is the first case that two lines of code.

2. Write a no-argument constructor subclass accessible, and it is the same, the subclass can not write what is called a default mechanism.

3. Write a constructor parameter has no write, no arguments constructor of the parent class is not a subclass can access the constructor with no arguments, a subclass must be stated in the first sentence in the sub-class constructor, call the parent class there constructor parameters, and the parameters passed into it.

4. The final statement and all class constructors are not within the purview of the subclass can not inherit access

 

In fact, as long as the class is inherited, whether abstract or entity, are required to meet this rule. Deleted at any time in succession this trial or prefixed abstract, the results did not change. Personally I feel that "the entity class must have explicit constructor" one really can not express this situation clearly, so the majority of job seekers still write more clearly better.

I like the wording is "can be inherited, but the successor entity and the same class, also called the parent class inheritable, and have access to the sub-class constructor." 

 

You can have static main method of abstract class.

public abstract class Test14 {

public static void main(String[] args) {
System.out.println("abc");
}
}

Operating results: abc

This program can be run, although you define a class Test14 is abstract. But you can not be in class there is no abstract methods, the class you actually non-abstract, they are able to run, if you define an abstract method fangfa (), and use this abstract method fangfa () in the main method used.

abstract modifier

Abstract class:

An abstract class can not be used to instantiate an object, the sole purpose of the statement of an abstract class is the class for future expansion.

A class can not be modified abstract and final. If a class contains abstract methods, the class must be declared as an abstract class, otherwise the compilation error will occur.

An abstract class may contain a non-abstract methods and abstract methods.

Abstract method

Abstract no method is a method of implementation, the specific implementation of the method provided by subclasses.

Abstract methods can not be declared as final or static.

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

If a class contains a number of abstract methods, the class must be declared as an abstract class. An abstract class may not contain abstract methods.

Abstract method declarations end with a semicolon, for example: public abstract Sample (); .

Guess you like

Origin www.cnblogs.com/helenwq/p/11655532.html