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

What is the use of java abstract functions?

little bottle
Release: 2019-05-29 12:02:55
Original
3082 people have browsed it

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() ;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template