How abstract is used in Java

Source: Internet
Author: User
Tags modifier

Abstract (abstract) modifier, you can modify classes and methods

The 1,abstract modifier class makes this class an abstract class that will not be able to generate object instances, but can be declared as object variables, i.e. compile-time types, and abstract classes, like the semi-finished products of a class, require subclasses to inherit and overwrite the abstract methods.

The 2,abstract modification method makes this method an abstract method, that is, only the declaration (definition) is not implemented, and the implementation part is replaced with ";". A subclass inheritance implementation (overwrite) is required.

Note: Classes that have abstract methods must be abstract classes. But abstract classes are not necessarily abstract methods, they can be all concrete methods.

The abstract modifier must be placed in front of the class name when the class is decorated.

The abstract modification method requires that its subclasses overwrite (implement) this method. The method that the subclass overrides (implements) can be invoked in polymorphic mode when invoked, which means that the abstract method must be implemented in its subclass, unless the subclass itself is also an abstract class.

Note: The parent class is an abstract class, where there are abstract methods, the subclass inherits the parent class, and all the abstract methods in the parent class are implemented (overwritten), the subclass has the ability to create an instance of the object, or the subclass must be an abstract class. An abstract class can have a constructor method, which is the constructor of the parent class (abstract class) that the subclass needs to invoke when it constructs the subclass object.

For a simple example, here's an abstract class

Abstract class e{

public abstract void Show ();

Public abstract can be omitted

}

Then the other class inherits it, usually in order to implement the method inside it

Class F extends e{

void Show () {

Code to write specific implementations

}

}

Finally, a polymorphic phenomenon occurs when a parent class reference is defined in the main method to point to a subclass object, such as

E e=new F ();

E.show ();

Actually called the show () method inside the subclass

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.