Abstract Class Initialization

前端 未结 2 705
渐次进展
渐次进展 2021-01-17 06:20

I have an abstract class:

abstract class Shape {

    public String color;
    public Shape() {
    }
     public void setColor(String c) {
        color = c         


        
相关标签:
2条回答
  • 2021-01-17 07:01

    No, you can never instantiate an abstract class

    0 讨论(0)
  • 2021-01-17 07:04

    initialization its still giving me an error but why

    Because the class is abstract. An abstract class simply can't be instantiated directly, whether or not it's got abstract methods. From the JLS section 8.1.1.1:

    It is a compile-time error if an attempt is made to create an instance of an abstract class using a class instance creation expression (§15.9).

    If you haven't got any abstract methods and you want to be able to instantiate the class directly, make the class non-abstract. The only reason to make a class with no abstract methods abstract is to force the use of concrete subclasses.

    0 讨论(0)
提交回复
热议问题