java-Preliminary Understanding-nineth-abstract class-Details

Source: Internet
Author: User
Tags define abstract

One.

An understanding of the abstract class as a whole.

1. Are there constructors in abstract classes?

For a problem, we give the answer at the same time, but also to explain the reason, then take the code to verify the reason.

Abstract classes cannot create objects , so constructors are not required. It was said that as long as the class one is defined, there is a default constructor. Cannot create an object, what is the use of the constructor? Although objects cannot be created, abstract classes have subclasses, but constructors in abstract classes can initialize their child class objects.

That is, the constructor of a class can either initialize the object of this class or initialize the subclass object.

The answer is yes, which is used to initialize the subclass object.

2. Abstract classes can not define abstract methods

OK.

Why is it? Abstract class There is no abstract method, then what is the meaning of abstract class?

There is, very rare. The purpose is to not allow the class to create objects . Why can't an abstract class create an object for this reason?

How this class behaves is unimportant and will be explained later.

Usually the method in this class has the method body, but there is no content. This means that {} is empty inside, as shown in.

Judge whether there is a method body, see there is no {}, in fact, there is still content, that is, return, but implicit. It and direct use ();

One is a method body, no content. The other is no method body.

The above classes, creating objects, calling Show1 (), and Show2 () have no meaning. What's the use of running it without content? So this kind of species is generally abstract.

Later, we will talk about why this kind of class, (Lenovo before the canine, Roar).

3. What keywords can I not coexist with abstract keywords?
Private, for example, precede the abstract keyword of the method with a private. I think it is not possible, private is not to provide, but the abstract class is to contact with the outside world, so can not add private (personal guess).

The truth is very simple, the abstract method is to be overwritten, a private word, the subclass will not know, do not know can not be covered. Therefore, it is not possible to be used with private ( This also means that in abstract methods, overrides are very important concepts .) DOS error is as follows,

Clearly tell me that the abstract need to cover, and now hide it, then how to cover.

Static can not, here is not to be used with the abstract keyword, abstract keywords are in the modification method. (Abstract keyword modifier three things, class, method, variable, here does not explain the premise of clear)

If this member becomes static, do you need an object? It is not necessary (static means that it can be called through a class, without requiring an object to be called). We all know that abstract methods run without meaning, because there is no method body, run it why, do not know what operation. So neither static nor abstract can coexist. ( the explanation here is that, as long as it is a static callout, it means that the class is going to call the class name .) )

Final no, the following two kinds are not possible, one is with the overwrite conflict, one is with its subclass conflict.

4. What are the similarities and differences between abstract and generic classes?

Do not think abstract classes are special, abstract classes and general classes are one thing. is a description of things, the general class has enough information to describe things, and abstract class information is insufficient.

Since it is abstract, it is of course not possible to create objects.

5. Abstract class must be a parent class?
Yes, if an abstract class is to be used, it must be overridden by a subclass of its abstract method (which is implemented in an abstract way), or it cannot be used.

These are some of the questions that are explained from the perspective of abstract classes, but whether there must be an abstract class is not necessarily.

java-Preliminary Understanding-nineth-abstract class-Details

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.