Summary of Java abstract classes

Source: Internet
Author: User
Tags logitech keyboard

What is abstract class:

When you define a parent class, often the method in the parent does not add anything, at this time if you call the parent in the subclass, if the class name or the method name is not written correctly in the subclass, there will be no execution, but this is not the default error, At this point, if the parent is defined as an abstract class, the subclass must have the contents of the parent class, that is, the wrong name will be an error, this time reflects the role of the abstract class.

To sum up two words, it is:

1. In the parent class and subclass, there are many cases where there is no content for the method of the parent class.

2. If the sub-class is rewritten, the normal method will not error.

Abstract class:

Classes that are decorated with abstract are called abstract classes, and abstract classes cannot be instantiated, that is, objects of an abstract class cannot be new.

Abstract method:

The method that is decorated with the abstract keyword is called an abstract method, and the abstract method must be defined in an abstract class.

Abstract methods have a declaration, the notation is not followed by curly braces, because the abstract method does not know who is to define, so can not write the function body.

 Public Abstract class Yunsuan {    protectedint  A;     protected int b;          Public Abstract void Qiuzhi ();}

Abstract class and abstract method relationships:

  Abstract methods must be defined in the abstract class.

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

If a class is an abstract class, the class can contain either abstract methods or concrete methods (declarations, implementations).

Abstract classes are also allowed if they are all concrete methods, and abstract classes can be empty, meaning nothing.

Inheritance of abstract classes:

When the parent class is an abstract class, the subclass inherits the parent class, and there are two cases where the subclass is an abstract class and the subclass is not an abstract class:

When subclasses are abstract classes:

Subclasses must also define an abstract keyword on class, subclasses can choose to implement or not implement the abstract method of the parent class, because the abstract class can contain abstract methods and concrete methods, so the subclass can also be written as a concrete method, or written as the whole abstract method, Either abstract methods or concrete methods are also available.

When subclasses are not abstract classes:

Subclasses are not abstract classes, this time the subclass will be all instantiated, that is, the parent class of the abstract method to implement all. The subclass does not have to use the abstract keyword at this time.

Specific uses of abstract classes:

An abstract method of an abstract class defines a specification, or a convention, that is given to subclasses for implementation.

Because the implementation of an abstract class may not be complete or meaningless.

Cases:

 Public Abstract class USB {    publicabstractvoid  qidong ();      Public Abstract void Tingzhi ();}

First define a USB abstract method as the parent class.

 Public class extends USB {    publicvoid  Qidong () {        System.out.println ("USB flash drive Started");    }     publicvoid  Tingzhi () {        System.out.println ("USB stick Stopped");}    }

Then define a upan as the instantiation subclass, and instantiate the contents of the parent class.

 Public Abstract  class extends USB {    publicvoid  Qidong () {        //  TODO Auto-generated method stub        System.out.println ("keyboard started up");;    }}

Define a Jianpan as an abstract subclass and implement only the Qidong within the parent class.

 Public class extends Jianpan {    publicvoid  Qidong () {        System.out.println ("Logitech Keyboard Boot");    }    @Override      Public void Tingzhi () {        //  TODO Auto-generated method stub        System.out.println ("Logitech Keyboard stopped");    }} 

Finally, a luojijianpan is defined as a subclass of Jianpan, the Tingzhi instantiated in the Jianpan inherited USB is not instantiated, and Jianpan in Qidong is rewritten.

 Public class Diannao {    publicvoid  Usbcharu (USB USB) {        USB. Qidong ();    }      Public void Usbbachu (USB USB) {        USB. Tingzhi ();    }}

 Public Static void Main (string[] args) {        new  Diannao ();                         New Luojijianpan ();        D.usbcharu (PAN);
D.usbbachu (pan); }

The output results are:

Logitech Keyboard starts Logitech Keyboard stopped

Summary of Java abstract classes

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.