What's the point in having an abstract class with no abstract methods?

后端 未结 9 1514
一整个雨季
一整个雨季 2021-01-02 21:11

Can have an abstract class implementing all of its methods-- with no abstract methods in it.

Eg.:

public abstract class someClass          


        
9条回答
  •  难免孤独
    2021-01-02 21:34

    The practical difference is that you can't create an instance of it. You would have to subclass it and create an instance of the subclass.

    As to WHY you would want to do this, in practice ... I'm hard pressed to think of a good reason. You could say that the class is only meaningful if someone creates a subclass that implements some function. But then why not make that function abstract in the super-class?

    I wouldn't rule out the possibility that someone might come up with some example where this makes sense, but I can't think of one. Just because it's possible to write a piece of code and that code compiles successfully doesn't mean that that it makes sense. I mean, I can write "total_price = item_price * zip_code + customer_height_in_cubits - 7.879", but that doesn't mean such a line of code would be meaningful.

提交回复
热议问题