Important Points about abstract class in Java
abstract
keyword is used to create an abstract class in java.
- Abstract class have
constructors
. We can run abstract class in java
like any other class if it has main() method.- Abstract class in java
can’t be instantiated
. - If abstract class doesn’t have any method implementation, its better to use interface because
java doesn’t support multiple class inheritance
.
- We can use abstract keyword to create an abstract method, an
abstract method doesn’t have body
. It’s not necessary for an abstract class to have abstract method
. We can mark a class as abstract even if it doesn’t declare any abstract methods.If a class have abstract methods, then the class should also be abstract using abstract keyword
, else it will not compile.
- Java
Abstract class can implement interfaces without even providing the implementation of interface methods.
- The subclass of abstract class in java
must implement all the abstract methods unless the subclass is also an abstract class
.
- Java Abstract class is used to provide
common method implementation
to all the subclasses or to provide default implementation
.
参考
abstract特征修饰符的作用
Abstract Class in Java.