Java-- abstract classes and interfaces

What is an abstract class

It is to be abstract after the modified class, parent class will feature common to all subclasses drawn up, he did not know the specific implementation of each sub-class of this feature, so no need to give a specific realization of common functions in the parent class, but given statement can be. The so-called statement given function, this functionality is abstracted, and then force the subclass must override the abstract function.

Precautions
  1. Once a class with an abstract method, then the subclass must be abstract.
  2. An abstract class can not abstract methods.
  3. Abstract methods can be both an abstract class, you may have non-abstract methods. Method abstract subclass override force. Non-abstract subclass inherits it.
  4. An abstract class can not be instantiated, can take the polymorphic instantiating indirectly.
  5. Subclass of the abstract class, or override all abstract methods of the parent class, or he is also an abstract class.
  6. There abstract class constructor for the subclass is used to initialize the parent class.

Keywords abstract can not coexist with those

  1. pravite: private, not inherit what is involved in the abstract
  2. final: The final modification methods, the method is not overridden. Modified class, the class can not be inherited.
  3. static: static word modification does not participate in inheritance, it is also meaningless.

What is the interface

interface, used to define a number of extensions, what things in the future want to have this feature, you can implement (implements) this interface, and then make specific function implementation.

Precautions
  1. Used to define a few extra features.
  2. Interface is used to define a number of rules (norms), extensions.
  3. Interface member variables are all static public constants. public static final default modifier
  4. No interface constructor.
  5. Interface is all abstract methods. public abstract default modifier.

The difference between abstract classes and interfaces

  1. Idea :
    an abstract class, to extract common feature of all subclasses, and forces subclasses for common functions to be rewritten.
    Interface definitions are some of the extra features among the entire inheritance hierarchy, which class would like to have these extensions, you can go to implement this interface.
  2. Syntax :
  • There abstract class constructor, interface no constructor.
  • Abstract methods can be both an abstract class, you may have non-abstract methods. The interface is all abstract methods.
  • Abstract class interface has member variables, you can have constant. Interface in full public static constants.
    Self-understanding
    because all the methods abstract class, regardless of not using all need to rewrite, and causing trouble in the writing process, and the interface in the process of implementation, does not need to rewrite that much, the interface can achieve more than , also make up the single inheritance nature of the deficiencies.
Published 15 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/Junzizhiai/article/details/102556919