What is the purpose of abstract classes in Java? [

2019-03-16 02:54发布

This question already has an answer here:

I have some questions around abstract classes and their use. I know the basics about them; for example, that they can't be instantiated, they can have concrete and abstract methods, ... But I guess what I wanna know is, what purpose do they serve in life (in software engineering)?

What is the purpose of abstract classes in Java? Why and when should one use an Abstract class? If you can use a normal class and then inherit it, why would you inherit an abstract class? How would using abstract classes make our life easier? would it provide better maintainability? more flexibility? ...

btw, I've already looked at some similar questions and answers, including Understanding the purpose of Abstract Classes in Java, but they don't answer my question. I'm more looking for answers that shed light on the philosophy behind the introduction of abstract classes in the first place in Java.

Thanks

1条回答
迷人小祖宗
2楼-- · 2019-03-16 03:52

An abstract class can be used as a type of template for other classes, and most commonly used for inheritance.

The best example is from the book head first java:

abstract class Animal(){}

then you can extend child class such as: dog, cat, fish.

查看更多
登录 后发表回答