Recently a little friend asked me what is abstract class, he has been confused about abstract class, let me tell him in a popular way. Emmmmmm, I asked him: What have you done with abstract classes? His answer was predictable: none at all. This makes it more difficult for me, I don’t know where to start to teach him science.
Then I asked: What do you know about abstract classes? He: I just know it can’t be new. Then I thought: do you know why it can’t be instantiated by new? He: It looks like… (This is a little confusing, afraid to confuse you)
The problem
He said a lot, and the questions I heard were:
- Confusion with interface concepts
- Lack of understanding of the use of abstract classes
The answer
All right, so let’s solve this problem today.
- What are the differences between abstract classes and interfaces in Java?
- A lack of understanding of the use of abstract classes is, in my opinion, a lack of use. If you have used the development pattern of abstract class factories, you will have a deep understanding of abstract classes. I wrote a simple demo:
Phone.class
/**
* Created by Fant.J.
*/
public abstract class Phone {
void print(){
System.out.println("this is a Phone abstract class method");
}
abstract void show();
}
Copy the code
PhoneMi.class
/**
* Created by Fant.J.
*/
public class PhoneMi extends Phone {
@Override
void show() {
System.out.println("my name is mi"); }}Copy the code
The Test class Test. Class
/**
* Created by Fant.J.
*/
public class Test {
public static void main(String[] args) {
Phone phone = new Phone() {
@Override
void show() {
System.out.println("my name is mi"); }}; phone.show(); Phone phone1 = new PhoneMi(); phone1.show(); phone1.print(); }}Copy the code
The execution result
my name is mi
my name is mi
this is a Phone abstract class method
Copy the code
Answers to questions:
Now that these questions are clear, it becomes clear why an Abstract class cannot be instantiated.
1. How can the print method in the Phone class be called?
When a subclass inherits Phone, it inherits the print method, which can be called from the subclass object
2. What will happen to the new Phone?
The first line of the main method in my test class is rewriting the Phone class. Instantiating Phone doesn’t work. Overriding this class is essentially an inherited implementation of the class. That is, I have overridden the show method, which is exactly the same as the show method in PhoneMi, so we can say that we have instantiated a new subclass object phone by implementing an inner class. From phone, we can also call the print method, which is essentially a subclass implementation of phone. Not the abstract class itself.
3. Why can new Phone subclass PhoneMi
It makes sense for us to instantiate PhoneMi as a subclass of Phone. As a subclass of Phone, PhoneMi must inherit the abstract class methods. If we override the abstract class methods, we can use PhoneMi as a normal class. Unlike ordinary classes, it can call methods of abstract classes (superclasses).
If I write a normal class with show methods and print methods, wouldn’t it be simpler? I: EMmmmm (in the heart ten thousand grass mud horse, I front a pile is white say? I think the abstract class itself is a design pattern. It exists for better design. Didn’t you say before that you never use it to complete tasks? Yeah, that’s the idea. If you really want to solve the problems of redundancy, efficiency, and clean code architecture, then you should think about design patterns.
All right, see? Do I need to post the last few words of courtesy? If you’re not familiar with it yet, leave a comment below and let me know when you see it. Thank you very much!