Java Abstract class and java Abstract

Source: Internet
Author: User

Java Abstract class and java Abstract

What is an abstract class? This name is very abstract. The first time I heard this name, it may be blocked. However, as the old man said, all anti-sprees are paper tigers, and the concept of dressing x is also a paper tiger. Well, we have already put down on it strategically. Now we need to attach importance to it tactically, just as if we want to solve paper tigers, We need to knock one tooth and pull one paw; the same is true for solving this abstract concept. First, we need to embody it, differentiate it, and then come one by one.

I usually have three questions when I encounter a new concept:

1. What is the purpose of this item? What is it? Where does it mean? (Obviously, if it is useless, there is no need to waste time; in fact, if you understand this problem, you will be able to grasp the problem by 60%)

2. How to use this concept or skill point? That is, its representation, such as keywords, modifiers, and syntaxes... (20%)

3. What are the key points and details of this application? (Yes, It also accounts for 20%)

The above three questions have been clarified, and the rest is used... "No, but he is familiar with it ."

 

I. First question: what is the use of abstract classes? What does it mean?

Before answering this question, let's take a look at an example in the Animal community: first, there is a parent class Animal, and then there are two sub-classes: Bird and Dog, respectively, as shown below:

1 public class Animal {2 public void bark () {} 3} 4 public class Bird extends Animal {5 public void bark () {6 System. out. println (" ~ Haha ~ "); 7} 8} 9 public class Dog extends Animal {10 public void bark () {11 System. out. println (" Wang ~ Wang ~ "); 12} 13}

As you can see, the parent class Animal has a calling method bark (). Both sub-classes inherit this method and rewrite it. The Bird class is screaming, and the Dog class is screaming, the question now is how does Animal call it? What kind of sound should be output in its bark () method body? Is it "Wangwang" or "whack "?
Obviously, animals are abstract collection nouns and we don't know how Animal is called. Therefore, the bark () method cannot be implemented in the parent class, or the implementation has no meaning, the bark () method can only be implemented in sub-classes based on the actual situation. In this way, the bark () method in the parent class Animal can be declared as abstract methods. At this time, this class also becomes abstract class.

Now, you can answer the first question: what is the abstract class used? Abstract classes cannot be instantiated by themselves. They exist in order to inherit the subclass. For a parent class, its implementation of a method in the parent class has no significance and must be implemented in the subclass according to the actual situation. Then, this method can be declared as abstract method, at this time, this parent class also becomes abstract class. (Of course, you may think, just like the above, is it okay if there is no space in the flower arc of the function? Yes, there is no syntax problem or even usage problem, but it is generally abstracted into abstract methods. There are three reasons: 1. as mentioned above, it makes "implementation has no meaning". 2. the content of the function body is not encouraged to be empty in Java; 3. after the child class inherits the parent class, the Child class will be forced to override the abstract method in the parent class to serve as a reminder and constraint .)

2. Question 2: How to Use abstract classes? What is the form?

This problem is relatively simple, that is, some regulations of the Language designers. in Java, abstract methods and abstract classes are modified with abstract. The above Animal class is written as follows:

1 public abstract class Animal{2     public abstract void bark();3 }

Iii. Third question: what are the key points in the process of using abstract classes?
An abstract class can not only contain abstract methods, but also contain common member methods and member variables. It has three main differences with common classes:

1. abstract methods in abstract classes can only be modified using public or protected. Because Abstract METHODS come to the world to allow subclass inheritance to be overwritten, while private attributes cannot be inherited by quilt classes. Obviously, there are contradictions.

2. abstract classes cannot create objects, that is, they cannot be instantiated. Abstract classes contain abstract methods that are not implemented and are incomplete. Therefore, they cannot be used to create objects. (In a special case, there is no abstract method in a class, but the class has abstract modification and is declared as an abstract class, so this class is also an abstract class and cannot be instantiated .)

3. If a class inherits from an abstract class, the subclass must implement the abstract method of the parent class. Otherwise, the method inherited by the subclass is still an abstract method, and the subclass is also an abstract class, which must be abstract. (This is like a parent's childhood have a dream, that is, taking an exam in college. However, due to the age, environment, personal ability, and other factors of their lives, it was not realized in a word, so they inherited the dream and asked them to implement it. As for whether you are admitted to Tsinghua University or Yangtze University, Let it be... Of course, if the son does not implement it, it will become an abstract class, and then let the son inherit and implement ...)

In other aspects, the abstract class is no different from the common class. Finally, let's look at another example:

1 public abstract class Animal {// abstract class can have non-abstract methods or member variables 2 private int a = 10; 3 4 public abstract void bark (); // if this abstract method is not available, but the class is modified with absract, it is also an abstract class and cannot be instantiated with 5 public void say () {// common member Method 6 System. out. println ("I am a non-abstract method in the abstract class, Private member variable a =" + a) in this abstract class; 7} 8 9 public int getA () {10 return; 11} 12 public void setA (int a) {13 this. a = a; 14} 15} 16 public class Dog extends Animal {17 public void bark () {// subclass Abstract method for implementing Animal 18 System. out. println ("Wang ~ Wang ~ "); 19 System. out. println ("I Am a subclass and cannot directly call the private variable a of the parent class :("); 20 System. out. println ("I Am a subclass, only through super. getA () calls the private variable a of the parent class: "+ super. getA (); 21} 22} 23 public class Test {24 public static void main (String [] args) {25 Dog = new dog (); 26 Dog. say (); // subclass inherits 27 dogs from the common member method that calls Animal. bark (); // subclass calls the implemented Method 28} 29}

 

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.