Home  >  Article  >  Java  >  What is the use of java abstract functions?

What is the use of java abstract functions?

little bottle
little bottleOriginal
2019-05-29 12:02:553057browse

What is the use of java abstract functions?

Abstract function: There is only the definition of the function, but there is no function body.

//没有花括号 { }
abstract void fun() ;

Syntactic features of abstract classes:

1. Abstract classes cannot generate objects.

why? If an object can be generated, the abstract method can be called through the object. However, if the abstract method does not have a method body, an error will occur.

2. If a class contains abstract functions, the class must be declared as an abstract class.

3. If there is no abstract function in a class, it can also be declared as an abstract class.

4. When there is a class and we don’t want others to generate its objects, we can define it as an abstract class. This type of class, also called a base class, is designed for inheritance.

Can an abstract class have a constructor?

Abstract classes can have constructors. In the subclass, if there is a constructor (because the subclass does not need to be an abstract class), then when the system calls the constructor of the subclass, it will automatically add a line of super();, that is, it calls the constructor of the parent class.

The role of abstract classes

If objects cannot be generated, what should we do with abstract classes?

Abstract classes are born to be parents, that is, to be inherited. Because its subclasses can override abstract functions to generate objects.

The above is the detailed content of What is the use of java abstract functions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn