Abstract class for Java

Source: Internet
Author: User

While abstract classes cannot achieve what we need to achieve individually, it can provide a great benefit to us in achieving the required functionality.

What is an abstract class?

In object-oriented concepts, all objects are depicted by classes, but conversely, not all classes are used to depict objects, and if a class does not contain enough information to depict a specific object, such a class is an abstract class.

Abstract classes In addition to the object cannot be instantiated, other functions of the class still exist, member variables, member methods and construction methods are accessed in the same way as the normal class.

Because an abstract class cannot instantiate an object, an abstract class must be inherited in order to be used. For this reason, it is often decided at the design stage to not design abstract classes.

The parent class contains a common method for the collection of subclasses, but because the parent class itself is abstract, these methods cannot be used.

in the Abstract classes in Java represent an inheritance relationship in which a class can inherit only one abstract class, while a class may implement multiple interfaces.

and, if the subclass of the abstract class is inherited and does not implement all the methods of the abstract class, it is still an abstract class.

Features of abstract classes:
1, abstract classes do not necessarily contain abstract methods, but classes with abstract methods must be abstract classes.

2. abstract methods in abstract classes are simply declarations, without method bodies, that is, the concrete implementation of the method is the specific function of the method

3, both abstract methods and abstract classes must be decorated with the abstract keyword.
4, abstract classes can not create and instantiate objects with new. Because the abstract class itself is incomplete.
5, abstract methods in the abstract class to be used, must be a subclass of all of the abstract methods, after the creation of child class object invocation.

6, abstract class can not be instantiated, if instantiated, will be error, compilation cannot pass. Only non-abstract subclasses of abstract classes can create objects.

Use of the suction class

Abstract classes need to set an abstract parent class and set the interface for each method. The subclass is then set to inherit the parent class and implement the specific methods in the parent class. If there is a requirement, you can design an abstract subclass to implement some of the methods of the parent class, and then set subclasses of the subclass to implement the incomplete method.

Benefits of abstract Classes

Abstract classes can combine a series of interconnected classes to form a class with multiple functions and identities. Abstract classes can make the programmer's mind clearer and the format of the program more organized. Each seed class can implement many different functions for the same function name, which is used for many different objects, so that they are no longer single, and they are interconnected with each other. It is also the equivalent of integrating different classes of functions to make programmers ' programming more simple and organized.

Abstract class for Java

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.