On Java abstract class

Source: Internet
Author: User

What is an abstract class? The name sounds very abstract, and the first time you hear it, it's really possible to be fooled. But, as the old man said, all reactionaries are paper tigers, all have the concept of dress x name is also paper tiger. Well, we've managed to defy it strategically, and now we have to focus on it tactically, like a paper tiger, a tooth, a tooth, a paw, a claw. To solve this kind of abstraction, the first thing to do is to make it concrete, finely differentiate, and then one by one.

I usually come across new concepts and ask three questions:

1. What's the use of this thing? What do you use it for? What is the meaning of it? (Obviously, if it is useless, there is no need to waste time; in fact, to understand this problem, mastered the 60%)

2. How does this concept or skill point work? That is, its manifestation, such as keywords, modifiers, grammar, etc... (This accounts for 20%)

3. What are the key points and details in the process of using this thing? (yes, also accounted for 20%)

The above three questions are clear, the rest is to use ... "Without him, but hand-cooked." ”

One, the first question: what is the use of abstract classes? What is the meaning of its existence?

Before answering this question, take a look at an example in the animal kingdom: First, there is a parent class animal, followed by two sub-classes, namely bird bird and dog dogs, as follows:

1  Public classanimal{2      Public voidbark () {}3 }4  Public classBirdextendsanimal{5      Public voidbark () {6System.out.println ("Haw ~ Haw ~");7     }8 }9  Public classDogextendsanimal{Ten      Public voidbark () { OneSystem.out.println ("Bark ~ Bark ~"); A     } -}

As you can see, the parent class animal has a calling method bark (), two subclasses inherit this method, and are rewritten, bird is haw, dog is barking, now the question is animal how to call? What kind of calls should be output in the body of the bark () method, "bark" or "haw"?
Obviously, the animal is an abstract set of nouns, we do not know how the animal animal, so, the bark () method in the parent class can not implement, or realize that there is no meaning, bark () method only in the subclass according to the specific situation to implement. In this way, the bark () method in the parent class animal can be declared as an abstract abstraction, and this class is also an abstract class.

At this point, you can answer the first question, what is abstract class used for? an abstract class cannot be instantiated by itself, and its existence is meant to allow subclasses to inherit. for a parent class, one of its methods is implemented in the parent class without any meaning, and must be implemented in the subclass according to the situation, then this method can be declared as an abstract abstraction, when this parent class is also an abstract class . (Of course, you might think, as above, the function's curly brackets are empty.) Yes, there is no grammatical problem, there is nothing wrong with the usage, but it is generally abstracted as an abstract method. There are three reasons: 1. As said above, so that "achieved no meaning"; 2. Java does not encourage the contents of the function body is empty; 3. After the subclass inherits the parent class, the subclass is forced to override the abstract method in the parent class, which acts as a reminder and a constraint. )

Secondly, the second question: How to use an abstract class? What is the form of expression?

This is a relatively simple question, which is the language designer's rule, which specifies that abstract methods and abstract classes be modified in Java. The animal class above is written in the following form:

1  Public Abstract class animal{2      Public Abstract void bark (); 3 }

Thirdly, the third question: What are the key points in the process of using abstract classes?
Abstract classes do not only contain abstract methods, they can also contain normal member methods and member variables. It differs from the common class in three main points:

1. Abstract methods in abstract classes can only be decorated with public or protected. Because, abstract method came to the world to let subclass inherit rewrite, and the method of private property can not inherit the quilt class, obviously contradiction.

2. Abstract classes cannot create objects, that is, they cannot be instantiated. Because an abstract class contains an abstract method that is not implemented, it is incomplete and cannot be used to create an object. (There is a special case where there is no abstract method in a class, but class classes are abstract and declared abstract, and this class is also an abstraction class and cannot be instantiated.) )

3. If a class inherits from an abstract class, the child class must implement the abstract method of the parent class. Otherwise, the subclass inherits the method or the abstract method, the subclass also becomes the abstract class, wants to use the abstract adornment. (This is like parents from a dream, is the university, but because of their life, environment, personal ability and other factors, in short, they did not achieve, so they will this dream let their children inherit, and ask them to achieve, as for you specifically to test Tsinghua University, or the Yangtze River, then let it be ... Of course, if the son does not realize, it will become an abstract class, and then let the grandson inherit and realize ... )

In other respects, there is no difference between an abstract class and a normal class. Finally, let's take another example:

1  Public Abstract classAnimal {//Abstract classes can have non-abstract methods, or you can have member variables2     Private intA = 10;3     4      Public Abstract voidBark ();//if this abstract method is not available, but the class is absract-modified, it is abstract and cannot be instantiated5      Public voidSay () {//Common member Methods6System.out.println ("I am a non-abstract method in an abstract class, the private member variable in this abstract class a=" +a);7     }8 9      Public intGeta () {Ten         returnA; One     } A      Public voidSetA (inta) { -          This. A =A; -     } the } -  Public classDogextendsanimal{ -      Public voidBark () {//a subclass implements an abstract method of animal -System.out.println ("Bark ~ Bark ~");  +System.out.println ("I am a subclass, I cannot call the private variable of the parent class directly A: (");  -System.out.println ("I am a subclass, only through Super.geta () call the parent class's private variable A:" +Super. Geta ());  +     } A } at  Public classTest { -      Public Static voidMain (string[] args) { -Dog dog =NewDog (); -Dog.say ();//subclass inherits the normal member method of calling Animal -Dog.bark ();//subclasses invoke methods that have already been implemented -     } in}

Talking about Java abstract classes

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.