Java4android (abstract classes and abstract functions)

Source: Internet
Author: User

The grammatical characteristics of abstract functions

Syntax characteristics of abstract classes

The role of abstract classes

Abstract classes are used to be inherited.

1. What is abstract function

Only the function is defined, the function without the function body is called abstract function;

abstract void fun ();

2, what is abstract class (commonly referred to as the base class)

Abstract class person{}

Classes defined using abstract are referred to as abstract classes;

(1) Abstract classes are not able to generate objects;

(2) If a class contains abstract functions, then the class must be declared as abstract class;

(3) If there is no abstract function in a class, then the class can also be declared as an abstract class. (sometimes let this class not produce objects)

3. Can the abstract class have a constructor function?

Yes

Abstract class person{    String name;     int Age ;        Person () {        System.out.print ("constructor for person");    }         void introduce () {        System.out.print (name+ "  " + Age);    }         Abstract void
class extends person{        Chinese () {// generate subclasses must first call the constructor of the parent class super ();        System.out.print ("Chinese constructor");    }         // the abstract function    in the replication person abstract class void eat () {        System.out.print ("Eat with Chopsticks");}    }

class test{    publicstaticvoid  main (String args[]) {        new Chinese ();        P.eat ();    }}


Why use abstract classes?

There are many features

For example, after defining an abstract class, let subclasses inheriting this abstract class must implement abstract functions in the abstract class.

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.