Are there any reasons to have an abstract class with every method in the class defined?

后端 未结 7 637
名媛妹妹
名媛妹妹 2021-02-05 01:42

It seems that an abstract class means the definition of the class is not complete and hence cannot be instantiated.

And I saw some simple Java code which has an abstract

7条回答
  •  猫巷女王i
    2021-02-05 02:25

    Yes, there is. Sometimes you know you're creating an abstract class - one that will have to be derived from to make any actual sense, but you want to provide a default implementation to all the methods. This doesn't happen a lot, but it does happen.

    UPDATE: I just had a case like this. I'm recording various user generated events. Each event has a TimeSpan and a Description, and they all have other things as well. I've created a base event class:

    public abstract class BaseEvent
    {
        public TimeSpan EventTime {get; private set;}
        public string Description {get; protected set;}
    
        public BaseEvent(TimeSpan time, string desc) ...
    }
    

    Granted, this is C# and not Java, but had I written this in Java, I would have done exactly the same thing)

提交回复
热议问题