Abstract usage in java

Source: Internet
Author: User
Abstract modifier, which can be used to modify classes and Methods

1. Abstract modifier class will make this class an abstract class. This class will not generate an object instance, but it can be used as the type of object variable Declaration, that is, the compile-time type, abstract classes are like a class of semi-finished products, which must inherit and overwrite the abstract methods.

2. Abstract modifier will make this method become an abstract method, that is, only declarations (definitions) are not implemented, and the Implementation part is replaced. Subclass inheritance is required (overwrite ).

Note: classes with abstract methods must be abstract classes. However, not all abstract methods are abstract methods, but also specific methods.

The abstract modifier must be placed before the class name.

Abstract modification requires that its subclass overwrite (implement. You can call the method after the subclass overwrites (implement) in a multi-state manner. That is to say, the abstract method must be implemented in its subclass unless the subclass itself is also an abstract class.

Note: If the parent class is an abstract class with an abstract method, the subclass inherits the parent class and implements (overwrites) All the abstract methods in the parent class, A subclass can create an instance of an object. Otherwise, the subclass must be an abstract class. Abstract classes can have constructor methods, which are the constructor of the parent class (abstract class) that a subclass needs to call when constructing a subclass object.
In a simple example, there is an abstract class.
Abstract class E {
Public abstract void show (); // public abstract can be omitted
}
Then, if other classes inherit from it, they usually implement the methods in it.
Class F extends E {
Void show (){
// Write the specific implementation code
}
}
Then define a parent class reference pointing to a subclass object in the main method to cause polymorphism, such
E = new F ();
E. Show ();
Actually, the show () method in the subclass is called.

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.