Abstract Functions and abstract classes. abstract classes of Abstract Functions

Source: Internet
Author: User

Abstract Functions and abstract classes. abstract classes of Abstract Functions

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.


What are abstract classes and abstract functions?

Abstract Functions: only functions are defined. functions without function bodies are called abstract functions.
Abstract void fun ();
1. abstract classes cannot generate objects
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, this class can also be declared as an abstract class.
Abstract classes cannot generate objects, but they can have constructors.
File Printer. java
Abstract class Printer {
Void open (){
System. out. println (open);} void close (){
System. out. println (close);} abstract void print ();}
The member function in the parent class is defined as an abstract function. If the member function is not rewritten in the subclass, the member function cannot be compiled. In this way, the method in the subclass can be avoided.
File HPPrinter. java
// The printer is an inkjet printer.
Class HPPrinter extends Printer {
Void print (){
System. out. println (printed using an inkjet printer );}}
File CanonPrinter. java
// The printer is a dot matrix printer.

Java Abstract class functions

Abstract classes cannot be instantiated, so the phrase "when I execute the Crazy function in the Animal class" is itself wrong. In addition, abstract methods are not written like this in abstract classes. It should be abstract void Cry (); instead of the abstract void Cry {} You wrote {};

In addition, the abstract class can only inherit from the quilt class, And the subclass overrides the abstract methods in the abstract class. The subclass can only be instantiated. Of course, the Cry method corresponding to the subclass is called. For example:
Class Pig extends Animal {
Void cry {
System. out. println ("Pig cry ");

Class Dog extends Animal {
Void cry {
System. out. println ("Dog cry ");
Public class Test {
Public static void main (String args []) {
Pig p = new Pig ();
Dog d = new Dog ();
P. cry ();
D. cry ();

Related Article

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.