Explain the difference between Java abstract class and ordinary class in detail

  • 2021-09-16 06:55:33
  • OfStack

On Abstract Classes

In the object-oriented concept, all objects are described by classes, but conversely, not all classes are used to describe objects. If there is not enough information in a class to describe a concrete object, such a class is an abstract class.

Seeing this may still find it difficult to understand. Give an example to illustrate 1: What do you think of when you talk about animals? Cats, dogs, chickens, ducks and geese? Of course, all these can be done. So, can you identify a specific object for the word animal? Obviously not. Even more strictly speaking, what do you think of when you talk about cats? Orange cat, short beauty …

After all:

There are 1,000 Hamley meows in 1,000 people's hearts.

Therefore, in our design, animals can be designed as abstract classes, while a specific species can be realized by inheriting animals.

I understand this part of polymorphism for a long time, one day suddenly will be used, also understand the so-called parent class reference to subclass object is what it means. But after writing, I found that I understand that speaking out is still very vague. Polymorphism is really very important, very important a point, to understand this part.

What is the difference between abstract classes and ordinary classes?

Grammatical rules of abstract classes

Syntax rules for abstract classes: abstract Keyword modification

1. Abstract classes cannot be instantiated.

2. Abstract classes may have abstract methods.

If there are abstract methods in 3.1 classes, then this class 1 must be an abstract class.

4. There can be ordinary properties, methods, static properties, and static methods in abstract classes.

5. Constructors can exist in abstract classes.


public abstract class AbstractObject{
	//  Common attribute 
    String name;
	//  Construction method 
    public AbstractObject(String name) {
        this.name = name;
    }
    //  Static method  -  Class name access 
    public static void staticMethod(){
        System.out.println(" Abstract classes can have static methods .");
    }
	//  Abstract method 
    public abstract void move();
	//  Common method 
    public void commonMethod(String food){
        System.out.println(" Abstract classes can have common methods .");
    }
}

Abstract classes cannot be instantiated

This part can remember the conclusion directly and temporarily, and the whole process can be skipped for the time being. According to my learning experience (the foundation is so poor), this part will be stupid when I look directly.

Define an abstract class of animals. Animals have to move (no! ) So define a common move () method.


public abstract class Animal {
    String name;
    public Animal(String name) {
        this.name = name;
    }
    public abstract void move();
}

When I wrote the example with IDEA, the second situation appeared directly! Damn it, abstract classes new It's coming out!


public class Test {
    public static void main(String[] args) {
    	//  Abstract classes cannot be instantiated ! Compile-time errors are reported directly !
        // Red-marked information : 'Animal' is abstract; cannot be instantiated
        Animal animal = new Animal(" Kitten ");
        //  No. 1 2 A situation 
        Animal animalObjcet = new Animal(" Kitten ") {
            @Override
            public void move() {
                System.out.println(" I'm starting to move !");
            }
        };
    }
}

Explanation of Case 2-Extended Knowledge: Anonymous Inner Classes (Skipped)

There is a knowledge point involved here called anonymous inner class.
The format of anonymous inner classes is as follows:


new  Class name or interface name (){
	 Override method ;
}
//  Put 1 From a comparative point of view , Obviously, what follows is 1 Anonymous inner classes 
new Animal(" Kitten ") {
    @Override
    public void move() {
        System.out.println(" I'm starting to move !");
    }
};

Anonymous: This class has no name
Inner class: A class that exists inside a class.

It actually inherits and implements a subclass of the Animal abstract class. That is to say, the Animal class is not instantiated here. This simple writing is equivalent to the following writing.


public class AnimalObject extends Animal{
    public AnimalObject(String name) {
        super(name);
    }
    @Override
    public void move() {
        System.out.println(" I am 1 Animals that can only move !");
    }
}

public class Test {
    public static void main(String[] args) {
        AnimalObject animalObject = new AnimalObject(" I am a subclass of animal abstract class ");
        animalObjcet.move();	//  I am 1 Animals that can only move !
    }
}

Subclasses of abstract classes

Note: There is one point that needs to be emphasized here. For methods in abstract classes, our words should be implemented. For methods that have been implemented, our words can be rewritten. After writing, I found that all my words in the previous description are rewritten.
Wrong writing: Do not override abstract methods in (Override) abstract classes
Correct writing: Do not implement abstract methods in (Implement) abstract classes
Added again: It seems that rewriting is not an error, and @ Override annotation is added in the automatic generation of IDEA. I won't continue to modify it.

1. Do not implement abstract methods in abstract classes

Subclass 1 is also an abstract class when an abstract method in an abstract class is not overridden. (Class 1 with an abstract method is an abstract class.)


public abstract class AbstractCat extends Animal{
    Integer weight;
    public AbstractCat(String name, Integer weight) {
        super(name);    //  Inherit the name of the parent class 
        this.weight = weight; //  The age of a cat 
    }
    //  This is not rewritten , It is still an abstract method 
    public abstract void move();
    //  Attention : The following writing is rewritten ! Only the method body is empty .
    // public void move(){};
}

2. Implement abstract methods in abstract classes

After implementing all the abstract methods in the abstract class, the cat class can now be a normal class.


public class Cat extends AbstractCat{
    public Cat(String name, Integer weight) {
        super(name, weight);
    }
    @Override
    public void move() {
        System.out.println("1 The weight that only runs is as high as " + weight + "kg Adj. " + name);
    }
}

Under Test 1:


public class Test {
    public static void main(String[] args) {
        Cat cat = new Cat(" Orange cat ", 20);
        cat.move();	// 1 The weight that only runs is as high as 20kg Orange cat 
    }
}

Ok, here, your orange cat finally runs!

On the Extension of Implementing Abstract Methods

I read many articles saying that subclasses should be rewritten (rewriting is wrong! Corrected here to implement) the abstract method of the parent class, abstract method. What if I only implement some abstract methods?
Step 1: Modify the Animal class


public abstract class Animal {
    String name;
    public Animal(String name) {
        System.out.println(" I am the construction method of animals !");
        this.name = name;
    }
    //  Add more abstract methods 
    public abstract void move();
    public abstract void eat();
    public abstract void sleep();
}

Step 2: Implement some abstract methods in the AbstractCat class


//  Do not add  abstract  Keyword will report an error 
// Class 'AbstractCat' must either be declared abstract or implement abstract method 'move()' in 'Animal'
public abstract class AbstractCat extends Animal{
    Integer weight;
    public AbstractCat(String name, Integer weight) {
        super(name);
        System.out.println(" I am the construction method of abstract cats !");
        //  Inherit the name of the parent class 
        this.weight = weight; //  The age of a cat 
    }
    @Override
    public void eat() {
        System.out.println(this.name + " Eating cat food ");
    }
    @Override
    public void sleep() {
        System.out.println(this.name + " Go to sleep !");
    }
}

Step 3: Cat class debut


public abstract class Animal {
    String name;
    public Animal(String name) {
        this.name = name;
    }
    public abstract void move();
}
0

Summary

1. Ordinary classes can be instantiated, but abstract classes cannot, because abstract classes are only a concept and cannot be mapped to concrete objects.

2. Both ordinary classes and abstract classes can be inherited, but after the abstract class is inherited, the subclass needs to override all the abstract methods in the abstract class, otherwise the subclass must be an abstract class.

Reference and extended reading

The difference between method overloading and method rewriting of Java basic series No.1 bomb
Characteristics of Java polymorphic member access in the second round of Java basic series
Java Basic Series 3 of the operation string classes are there? What's the difference?


Related articles: