Abstract Functions and abstract classes

Source: Internet
Author: User

A function consists of a function definition (return value type, function name, and parameter list) and a function body.

Abstract function: only functions are defined, and no function body is defined using the abstract keyword. For example

abstract void fun();

Cause of error: if one or more abstract functions exist in a class, the class must be defined as an abstract class.

What is an abstract class?

The class defined using abstract is called an abstract class (base class ).

[1] The constructor of the abstract class cannot be called to generate the object of the abstract class.

[2] If a class contains abstract functions, the class must be an abstract class.

[3] A class without abstract functions can also be an abstract class.

Abstract class: used for inheritance. An object can be generated by subclass of an abstract class.

After the subclass inherits the abstract class as the parent class, the abstract function of the abstract class can also be inherited. According to article [2], the subclass must be defined as an abstract class, however, in this case, the object cannot be generated. In addition, you can add the function body through the write-down abstract function, so that the object can be generated.

Abstract class person {person () {system. Out. println ("person constructor");} abstract void eat (); // abstract function, can also be inherited}

 

Class Chinese extends person {Chinese () {system. out. println ("Chinese constructor");} void eat () {// rewrite system. out. println ("eat with chopsticks ");}}

 

Class test {public static void main (string ARGs []) {person P = new Chinese (); // converts P. Eat ();}}

 

Can an abstract class have Constructors ??

Although the constructor of abstract classes cannot be called using new, the constructor of abstract classes can be called using super, so the conclusion is acceptable.

 

Abstract Functions and abstract classes

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.