"JAVA" abstract class detailed __java

Source: Internet
Author: User

Abstract the conceptual abstraction of a class is a special method: it has only declarations, but no concrete implementations. The declaration format for an abstract method is:

abstract void Fun ()//No curly braces means no body
Abstract methods must be decorated with the abstract keyword. If a class contains an abstract method, the class is called an abstract class, and the abstract class must be decorated with the abstract keyword before the class. Because an abstract class contains methods that do not have a specific implementation, you cannot create an object with an abstract class. Note: In the Java programming idea, the abstract class is defined as "a class containing abstract methods," but it is later found that if a class does not contain abstract methods, it is also an abstract class if it is modified in abstract. This means that an abstract class does not necessarily have to contain abstract methods.
The abstract class a{//defines a public
    void fun () {//Common method
        System.out.println ("Method of Existence of method") for an abstraction.
    public abstract void print ();//abstract method, no method body, with abstract keyword modifier
}
Explanation:As you can see, an abstract class exists for inheritance, and if you define an abstract class and do not inherit it, you create this abstract class in vain, because you cannot do anything with it. For a parent class, if one of its methods does not make sense in the parent class, it must be implemented differently depending on the actual requirements of the subclass, so that the method can be declared as an abstract method, at which point the class becomes an abstract class. Note:A class that contains abstract methods is called an abstract class, but does not imply that there can be only abstract methods in an abstract class, which, like ordinary classes, can also have member variables and ordinary member methods.

Ii. principles of Use of abstract classes

1 The abstract method must be public or protected (because if private, you cannot inherit the quilt class, the subclass will not implement the method), by default, public.

2 Abstract classes cannot be used to create objects, need to rely on subclasses to be processed in an upward transition;;//When a class is instantiated, it means that the object can invoke properties or behavior in the class, but there are abstract methods in the abstract class, and the abstract method has no method body and cannot be invoked without the method body. If you can't make a method call, how do you create an instantiated object?

3 The abstract class must have subclasses, using extends inheritance, a subclass can only inherit an abstract class, and if a class (generic class) inherits from an abstract class, the subclass must implement the abstract method of the parent class (note that the implementation is not equally defined as an abstract class). If the subclass does not have an abstract method that implements the parent class, the subclass must also be defined as an abstract class.


Third, other

(1) Is there a construction method in the abstract class?
Because there are some attributes in an abstract class, there must be a construction method in the abstract class that exists for the purpose of initializing the attribute.

And when the subclass object is instantiated, it still satisfies the order in which the parent class construction is performed first and then the subclass constructs.

(2) The abstract class can be declared in final.

No, because the abstract class must have subclasses, and the final defined class cannot have subclasses;


Other examples can refer to: http://blog.csdn.net/wei_zhi/article/details/52736350


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.