A detailed example of the java abstract class

  • 2020-09-16 07:30:00
  • OfStack

Example details of the java abstract class

Preface:

What is an abstract class? The name sounds abstract, and you can be fooled the first time you hear it. But, as the old man said, all reactionaries are paper tigers, and so is the concept of x. Well, we've done that strategically, and now we need to treat it tactically, just as we would solve a paper tiger by beating it tooth by tooth and pulling it paw by paw; The solution to this abstraction is to make it concrete, to subdivide it, and then to do it one by one.

I always ask three questions when I encounter a new concept:

1. What's the use of this? What is it for? What does it mean? (Obviously, if it's useless, there's no need to waste time; In fact, if you understand this question, you have mastered 60%.)

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

3. What are the key points and details in the process of using this product? (Yes, also 20%)

The above three questions have been cleared up, the rest is to use... "No, but my hand is familiar."

1. Question 1: What is the use of abstract classes? What is the point of its existence?

To answer this question, let's look at an example from the animal kingdom: First, there is a parent class Animal, then there are two subclasses, Bird for birds and Dog for dogs, as follows:


public class Animal{
  public void bark(){}
}
public class Bird extends Animal{
  public void bark(){
    System.out.println(" haw ~ haw ~");
  }
}
public class Dog extends Animal{
  public void bark(){
    System.out.println(" Wang wang ~ Wang wang ~");
  }
}

As you can see, the parent class Animal has one calling method, bark (). Both subclasses inherit and rewrite this method, Bird for chirp and Dog for bark. Now the question is what is Animal called? What should be the output of its bark () method, a "woof" or a "chirp"?

Obviously, animal is an abstract collective noun and we don't know what to call animal Animal, so the bark () method is not implemented in the parent class, or it doesn't make any sense to implement it. The bark () method can only be implemented in the subclass on a case-by case basis. This way, you can declare the bark () method in the parent class Animal as the abstract abstract method, and the class becomes the abstract abstract class.

At this point, we can answer the first question, what does an abstract class do? An abstract class cannot be instantiated by itself; it exists to allow subclasses to inherit. In the case of a parent whose method does not make sense to implement in the parent class and must be implemented in the child class, the method can be declared as abstract abstract method, in which case the parent becomes abstract abstract class. (Of course, you might think, like the above, that it's ok to have empty parentheses. Yes, there is nothing wrong with the syntax, even the usage, but it is generally abstracted as the abstract method. There are three reasons for this: 1. As said above, it "makes no sense"; 2. 2. Empty contents of function body are not encouraged in Java; 3. After the superclass inherits the superclass, the subclass will be forced to override the abstract methods in the superclass to serve as a reminder and constraint.

2. Question 2: How do abstract classes work? What is the form of expression?

The problem is relatively simple. It is the language designer's stipulation that abstract is used to decorate abstract methods and abstract classes. The Animal class above is written as follows:


 public abstract class Animal{
   public abstract void bark();
}

3. Question 3: What are the key points in using an abstract class?

Abstract classes do not have to contain only abstract methods; they can also contain normal member methods and member variables. It differs from ordinary classes in three main ways:

1. Abstract methods in abstract classes can only be decorated with public or protected. Since abstract methods are meant to be overridden by subclasses, it is a contradiction that the methods of the private property cannot be inherited by subclasses.

2. Abstract classes cannot create objects, i.e., cannot be instantiated. Because abstract classes contain abstract methods that are not implemented, they are incomplete and cannot be used to create objects. (There is a special case in which a class does not have abstract methods, but class class has the abstract decoration and is declared as an abstract class. This class is also an abstract class and cannot be instantiated.)

3. If a class inherits from an abstract class, a subclass must implement the abstract methods of the parent class. Otherwise, the subclass inherits an abstract method, and the subclass becomes an abstract class, decorated with abstract. (it's like parents since the childhood have a dream, is to get into college, but because they lived, and environmental factors such as, personal ability, anyhow don't realize, then they will inherit this dream let her children, and asked them to achieve, as for your specific is go to tsinghua university, still take an examination of the Yangtze university, then Let it be... Of course, if the son doesn't implement it, it will become an abstract class, and the grandson will inherit and implement it...)

In other ways, abstract classes are no different from ordinary classes. Finally, here's another example:


public abstract class Animal {  // Abstract classes can have either non-abstract methods or member variables 
  private int a = 10;
  
  public abstract void bark();      // If there is no such abstract method, however class before absract Modifier, also an abstract class, cannot be instantiated 
  public void say() {            // Ordinary member method 
    System.out.println(" I am a non-abstract method in an abstract class, a private member variable in this abstract class a= " + a);
  }

  public int getA() {
    return a;
  }
  public void setA(int a) {
    this.a = a;
  }
}
public class Dog extends Animal{
  public void bark() {        // The subclass implementation Animal Abstract method of 
    System.out.println(" Wang wang ~ Wang wang ~");  
    System.out.println(" I am a subclass and cannot call the parent's private variable directly a   : ( ");  
    System.out.println(" I'm a subclass, only pass super.getA() Call the parent class's private variable a : " + super.getA());  
  }
}
public class Test {
  public static void main(String[] args) {
    Dog dog = new Dog();
    dog.say();    // Subclass inheritance call Animal Ordinary member method 
    dog.bark();    // A subclass calls a method that has already been implemented 
  }
}

Above is the example of java abstract class details, if you have any questions, please leave a message or to this site community exchange discussion, thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: